The fragment which is got from backstack, call onC

2019-04-25 17:05发布

Please help me resolve my issue.

I have MainActivity with a framelayout. I want to replace some fragments into the framelayout. Now I am encounering an issues, it's:

1) I created and put fragment A to framelayout. Fragment A called onCreateView...etc.

2) Then I created and put fragment B to layout... Fragment A was put on backstack and it called onPause() (not called onDeattach(), onDestroy...)

3) I pressed back button. Fragment A was got from backstack, but it called onCreateView() again. This action make my app has some another issues.

So my question is how to store fragment A in backstack and it don't recreate view.

This's the method that was used to change fragment:

public static void setContent(FragmentManager managerFragment, Fragment detailFragment) {
    if (managerFragment != null) {
        if(lastFragment==null && detailFragment instanceof HomeVer3Fragment ||
                (lastFragment!=null && detailFragment instanceof HomeVer3Fragment && lastFragment instanceof HomeVer3Fragment)){
            return;
        }
        String tag=detailFragment.getClass().getName();
        managerFragment.popBackStackImmediate(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        FragmentTransaction ft = managerFragment.beginTransaction();
        ft.replace(R.id.content_frame, detailFragment);
        ft.addToBackStack(tag);
        ft.commit();
        lastFragment = detailFragment;
    }
}

Thank and sorry for my bad question, my english is not well.

1条回答
【Aperson】
2楼-- · 2019-04-25 17:42

check out my complete post over here Dynamically changing the fragments inside a fragment tab host?

As far your problem of reloading ( I assume ) you can do some thing like undermentioned:

(1) Initiate a Boolean value say

boolean android_hacker = false;

(2) Now say U need to fetch data and create some view using list view. Now at this stage set "android_hacker = true;" after you have fetched all data.

(3) Now when U again come back to same fragment say "FragmentA" then check value on OnCreateView as mentioned ..

if(android_hacker != true){
        new GoAsyncTask().execute();
        }else{
// Perform stuff U need
}

That's it. Hope it helps some one

查看更多
登录 后发表回答