记住 a 的生命周期fragment
。当我们回到这一点时,它将从oncreateView()
永远开始。但是我们仍然可以保存数据,然后我们将在 oncreated 中处理这些数据以填充我们的视图。对你来说,你可以这样做:
主要活动.java
public class MainActivity extends AppCompatActivity implements Fragment2.myListener {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate: ");
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment1 fragment1 = Fragment1.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment1, Fragment1.TAG);
fragmentTransaction.addToBackStack(Fragment1.TAG);
fragmentTransaction.commit();
}
});
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
Log.e(TAG, "onSaveInstanceState");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy");
}
@Override
public void bindCount(int newCount) {
((Fragment1)getSupportFragmentManager().findFragmentByTag(Fragment1.TAG)).setCount(newCount);
}
}
片段1.java
public class Fragment1 extends Fragment {
public static final String TAG = "fragment1";
private static final String SAVE_COUNT = "save_count";
private int count;
public Fragment1() {
}
public static Fragment1 newInstance() {
Fragment1 fragment = new Fragment1();
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate: ");
if (savedInstanceState != null) {
count = savedInstanceState.getInt(SAVE_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
Button goToButton = (Button) view.findViewById(R.id.button);
goToButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment2 fragment2 = Fragment2.newInstance();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment2, Fragment2.TAG);
fragmentTransaction.addToBackStack(Fragment2.TAG);
fragmentTransaction.commit();
}
});
return view;
}
public void setCount(int newCount){
count = newCount;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e(TAG, "onSaveInstanceState: ");
outState.putInt(SAVE_COUNT, count);
}
}
片段2.java
public class Fragment2 extends Fragment {
public static final String TAG = "fragment2";
public Fragment2() {
// Required empty public constructor
}
public static Fragment2 newInstance() {
Fragment2 fragment = new Fragment2();
return fragment;
}
myListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment2, container, false);
//Here I am just modifying a value that wants to send to fragment1
listener.bindCount(45);//newCount
return view;
}
public interface myListener{
void bindCount(int newCount);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//initialice listener
listener = (myListener) getActivity();
}
}
所以...对于我们之间的通信,fragments
我们将需要您的容器activity
,通过接口。正如我们所看到的,Fragment2 有一个interface
实现它的方法,当它被执行时,它会调用一个方法,我们更改存储在 中activity
的计数值,因此即使再次执行,我们也可以恢复发生的任何修改。这可以用于许多其他数据,例如等。fragment1
onSaveInstanceState
oncreateview
arraylist, string, float, long, object,
对不起我的“英语”!!!!!!