我如何保存textView值,同时从它的片段移到另一个片段,并在完成时取回该值?我试过(sharedPrefrences,savedInstanceState,show and hide),但没有任何帮助,在改变方向时都保存了碎片状态,但当我移动到其他碎片时没有保存....一个有4个按钮的主活动,每个按钮附加到一个新片段代表总共4个片段。
像这样
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final Button firstFrag;
firstFrag = findViewById(btn_first_frag);
Button secondFrag;
secondFrag = findViewById(btn_second_frag);
Button thirdFrag;
thirdFrag = findViewById(btn_third_frag);
Button fourthFrag;
fourthFrag = findViewById(btn_fourth_frag);
final SecondFragment secondFragment = new SecondFragment();
firstFrag.setText(getIntent().getExtras().getString("firstPlayer"));
secondFrag.setText(getIntent().getExtras().getString("secondPlayer"));
thirdFrag.setText(getIntent().getExtras().getString("thirdPlayer"));
fourthFrag.setText(getIntent().getExtras().getString("fourthPlayer"));
firstFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeFragments(new FirstFragment());
}
});
secondFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeFragments(new SecondFragment());
SharedPreferences preferences = getSharedPreferences("settings",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("value",secondFragment._screen.getText().toString());
String last = preferences.getString("value","0");
secondFragment._screen.setText(last);
}
});
thirdFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeFragments(new ThirdFragment());
}
});
fourthFrag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
swipeFragments(new FourthFragment());
}
});
}
public void swipeFragments(Fragment fragment) {
getSupportFragmentManager().beginTransaction().add(R.id.container,fragment).commit();
}
我希望在按下按钮移动到其他片断之前保存片断状态,并在按下前一个片断按钮时保留保存的状态
public final class MyFragment extends Fragment {
private TextView vstup;
private Bundle savedState = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.whatever, null);
vstup = (TextView)v.findViewById(R.id.whatever);
/* (...) */
/* If the Fragment was destroyed inbetween (screen rotation), we need to recover the savedState first */
/* However, if it was not, it stays in the instance from the last onDestroyView() and we don't want to overwrite it */
if(savedInstanceState != null && savedState == null) {
savedState = savedInstanceState.getBundle(App.STAV);
}
if(savedState != null) {
vstup.setText(savedState.getCharSequence(App.VSTUP));
}
savedState = null;
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
savedState = saveState(); /* vstup defined here for sure */
vstup = null;
}
private Bundle saveState() { /* called either from onDestroyView() or onSaveInstanceState() */
Bundle state = new Bundle();
state.putCharSequence(App.VSTUP, vstup.getText());
return state;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/* If onDestroyView() is called first, we can use the previously savedState but we can't call saveState() anymore */
/* If onSaveInstanceState() is called first, we don't have savedState, so we need to call saveState() */
/* => (?:) operator inevitable! */
outState.putBundle(App.STAV, (savedState != null) ? savedState : saveState());
}
/* (...) */
}
先生/女士,我想使用android JAVA将一个片段回调到另一个片段。我试图找到问题,但没有找到解决方案。若我使用接口,它会向活动发送回调响应,这是我不想要的。非常感谢。
这是我完整的logcat: 在Android.support.v4.app.backStackRecord.doaddop(backStackRecord.java:414)在Android.support.v4.app.backStackRecord.replace(backStackRecord.java:449)在Android.support.v4.app.backStackRecord.
我试着从一个片段到主要片段。编译器不喜欢我的代码,但我不确定是什么问题。转换片段的最佳实践是什么? //这是我要导航到的主片段的xml 2019-07-12 17:49:45.091 959 4-9594/com.example.cribb E/AndroidRuntime:致命异常:主进程:com.example.cribb,PID:9594 Android.content.res.resour
在我的中有5个片段(home、settings、newPost、notifications和profile)。 在中,我有一个,其中包含许多post视图,每个post都包含一个“open”按钮,该按钮应该打开一个。问题是,我只能在类和populateItemRows()方法中使用,所以当我不能使用... RecycerViewAdapter:
我需要一些暑期项目的帮助 这是我的事件片段 这是我的MyList片段 我正在使用RecyclerViewCardView显示事件。其想法是,用户可以单击每张卡片右侧的大加号,卡片将显示在MyList片段中。我想问一下,是否可以将一张卡直接从一个片段转移到另一个片段?而且,这两个片段都包含在同一个活动中,这使得它有点棘手(我还没有找到任何可用的解决方案)。 如果不可能,另一种方法是将CardView
问题内容: 我在小组活动中有一个片段,我想用另一个片段替换它: 在不使用活动组的情况下作为单独的项目完成时,它工作正常,当控件进入getview()时,每件事在日志猫中都可以正常工作,但是没有视图可见,甚至没有任何异常出现,我希望将书详细信息片段由部分详细信息片段代替。 图书详细信息片段的XML具有id book_description_fragment,而部分描述片段的xml具有id secti