简体   繁体   中英

How to open a fragment from another fragment

I have a tabbed activity in which I have three tabs. The main tab is the home tab and it contains a button. My question is how do I open a new fragment after pressing the button and keeping it in the same tabbed activity ? Here is my Home tab class code:

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * Created by user-pc on 15/01/2017.
 */


public class Home extends Fragment {
    private boolean flag;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.hometab, container, false);
        final Button RestButton = (Button) rootView.findViewById(R.id.RestButton);
        final Button ShopButton = (Button) rootView.findViewById(R.id.ShopButton);
        final Button HangoutButton = (Button) rootView.findViewById(R.id.HangoutButton);
        RestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return rootView;
    }
}

When you add a default tabbed activity in android you get an activity that uses ViewPager as a container for fragments managed by a FragmentPagerAdapter.

From FragmentPagerAdapter's documentation :

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs.

If I understand you correctly, you want one of the fragments, namely the Home fragment, which resides in a page (let's call it Page1) to be dynamically swapped with another fragment (let's call this new fragment FragmentB) and to persist as the new fragment if you switch to another page (let's call it Page2) and back to its page (meaning you will see FragmentB when switching back to Page1).

For that it seems like you are better off using FragmentStatePagerAdapter . Inside the fragment for Page1 - have a container for another fragment (a fragment within the fragment), occupying the whole area of the parent fragment. The first and default fragment inside it would be the Home fragment and it would be loaded dynamically in onCreateView method if there was no state saved before. Else - the loaded fragment would be according the saved state - or even from the saved state directly. To have that saved state - you need to implement the onSaveInstanceState(Buncle) to do have the saved state. Then you swap the Home fragment with FragmentB in inside the container fragment which is the one loaded every time into Page1 - but it remembers which fragment was loaded in it last - and can restore it correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM