Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The fragment which is got from backstack, call onCreateView() again

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.

like image 792
Leo Nguyen Avatar asked Dec 10 '13 07:12

Leo Nguyen


People also ask

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

What is onCreateView fragment?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .

What is Backstack in fragments?

Calling addToBackStack() commits the transaction to the back stack. The user can later reverse the transaction and bring back the previous fragment by pressing the Back button. If you added or removed multiple fragments within a single transaction, all of those operations are undone when the back stack is popped.

What are the differences between onCreate () onCreateView () and onActivityCreated () in fragments and what would they each be used for?

The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.


1 Answers

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

like image 135
AndroidHacker Avatar answered Oct 20 '22 17:10

AndroidHacker