Fragment calling fragments loosing state on screen

2019-06-13 06:11发布

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?

2条回答
倾城 Initia
2楼-- · 2019-06-13 07:08

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-06-13 07:09

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.

查看更多
登录 后发表回答