Fragment calling fragments loosing state on screen rotation

若如初见. 提交于 2019-12-08 13:19:01

问题


Hi i created a project with a default "Navigation Drawer Activity". So i have a MainActivity with a fragment with is replaced for each item on menu.

One of the menus is "Customers" with shows a list of customers.

From customers fragment i can see the Interests of this customers, with is a Fragment(CustomerListFragment) calling the interests(InterestsListFragment).

There is even more levels, but to be short that's enough.

This is the code on MainActivity that i use to call fragment from fragment and pass data between

public void passData(Object[] data, Fragment f) {
    Bundle args = new Bundle();
    args.putSerializable("PASSED_DATA", data);
    f.setArguments(args);
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, f)
            .addToBackStack("")
            .commit();
}

And i use like :

mCallbacks.passData(new Object[]{c}, new OpportunityListFragment());

The problem is that when i rotate the phone does not matter from wich level of activity i have, it comes back to the first fragment called(CustomerListFragment), and if i click "Back" on cellphone it gets back to where i was when i rotate the phone.

What do i have to do, to avoid this kind of problem? why it gets back to the first activity evoked if i am replacing fragments?


回答1:


The answer from ste-fu is correct but let's explore programmatically. There is a good working code in Google documentation @ Handling Runtime Changes. There are 2 code snippets that you have to do.

1) Code snippet:

public class MyActivity extends Activity {

private RetainedFragment dataFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // find the retained fragment on activity restarts
    FragmentManager fm = getFragmentManager();
    dataFragment = (DataFragment) fm.findFragmentByTag(“data”);

    // create the fragment and data the first time
    if (dataFragment == null) {

Note: Code uses FragmentManager to find the current Fragment. If fragment is null, then the UI or app has not been executed. if not null, then you can get data from RetainedFragment object.

2) Need to retain the Fragment state.

public class RetainedFragment extends Fragment {

// data object we want to retain
private MyDataObject data;

// this method is only called once for this fragment
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // retain this fragment
    setRetainInstance(true);
}

Note: setRetainInstance is used in OnCreate. And subclassing the Fragment is recommended, naming it RetainedFragment, used on snippet 1.




回答2:


When you change screen orientation your parent Activity is destroyed and recreated. Unless you persist the level structure in some fashion, it will always appear as when you first started the activity. You can either use the bundle object, or for more complicated objects you need to persist it to a database.

Either way, onSaveInstanceState is your friend. Then in your onCreate method you need to check the bundle or database, and the set the fragment accordingly.



来源:https://stackoverflow.com/questions/29262662/fragment-calling-fragments-loosing-state-on-screen-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!