Pratham Khurana
Pratham Khurana

Reputation: 9

Retaining fragments state in switching between them using BottomNavigationView

I have a Bottom Navigation View and in that I want to use the fragment and Navigation in such a way that the every time user click on section of navigation View ,fragment should load the previous state rather than creating new fragment. I have a Frame Layout and all fragment should load or replace in that Frame Layout. Here is my code opublic class MainActivity extends AppCompatActivity {

    BottomNavigationView navigation;
    Home home;
    Trending trending;
    Upload upload;
    ImageView uploadImage;
    final public static int UPLOAD_ACTIVITY_CODE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigation = findViewById(R.id.navigation);
        uploadImage = findViewById(R.id.uploadimg);
        home = new Home();
        trending = new Trending();
        upload = new Upload();
        fragmentLoad(home);
        uploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), UploadActivity.class);
                startActivityForResult(intent,UPLOAD_ACTIVITY_CODE);
            }
        });
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        fragmentLoad(home);
                        break;
                    case R.id.trending:
                        fragmentLoad(trending);
                        break;
                    case R.id.upload:
                        fragmentLoad(upload);
                        break;
                }
                return true;
            }
        });
    }

    public void fragmentLoad(android.support.v4.app.Fragment fragment){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame, fragment);
        transaction.commit();
    }

    public void fragmentLoad(android.support.v4.app.Fragment fragment,Bundle bundle){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().show(fragment);
        fragment.setArguments(bundle);
        transaction.replace(R.id.frame, fragment);
        transaction.commit();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==UPLOAD_ACTIVITY_CODE && resultCode==UPLOAD_ACTIVITY_CODE && data!=null){
            String desc = data.getStringExtra("Desc");
            String title= data.getStringExtra("Title");
            String photo= data.getStringExtra("PhotoUri");
            String duration= data.getStringExtra("duration");
            String video= data.getStringExtra("videoUri");
            Bundle bundle = new Bundle();
            bundle.putString("desc",desc);
            bundle.putString("title",title);
            bundle.putString("photo",photo);
            bundle.putString("duration",duration);
            bundle.putString("videoUri",video);
            fragmentLoad(upload,bundle);
            navigation.setSelectedItemId(R.id.upload);
        }
    }
}

Upvotes: 1

Views: 551

Answers (1)

aminography
aminography

Reputation: 22832

You should create an instance from each fragment, then show them according to bottom navigation state using FragmentManager. You should write a method named setData in Upload fragment class to reinitialize its view. I wrote a class for this case, I hope it helps you.

public class MainActivity extends AppCompatActivity  {

    public static final int UPLOAD_ACTIVITY_CODE = 100;
    private BottomNavigationView navigation;
    private ImageView uploadImage;
    private Home home;
    private Trending trending;
    private Upload upload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        navigation = findViewById(R.id.navigation);
        uploadImage = findViewById(R.id.uploadimg);
        home = new Home();
        trending = new Trending();
        upload = new Upload();

        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.getMenu().findItem(R.id.home).setChecked(true);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.frame, home)
                .add(R.id.frame, category)
                .add(R.id.frame, search)
                .commit();
        setTabStateFragment(TabState.HOME);

        uploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), UploadActivity.class);
                startActivityForResult(intent, UPLOAD_ACTIVITY_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UPLOAD_ACTIVITY_CODE && resultCode == UPLOAD_ACTIVITY_CODE && data != null) {
            String desc = data.getStringExtra("Desc");
            String title = data.getStringExtra("Title");
            String photo = data.getStringExtra("PhotoUri");
            String duration = data.getStringExtra("duration");
            String video = data.getStringExtra("videoUri");
            Bundle bundle = new Bundle();
            bundle.putString("desc", desc);
            bundle.putString("title", title);
            bundle.putString("photo", photo);
            bundle.putString("duration", duration);
            bundle.putString("videoUri", video);
            upload.setData(bundle);
            setTabStateFragment(TabState.UPLOAD);
        }
    }

    private void setTabStateFragment(TabState state) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (state) {
            case HOME: {
                transaction.show(home);
                transaction.hide(trending);
                transaction.hide(upload);
            }
            case TRENDING: {
                transaction.hide(home);
                transaction.show(trending);
                transaction.hide(upload);
            }
            case UPLOAD: {
                transaction.hide(home);
                transaction.hide(trending);
                transaction.show(upload);
            }
        }
        transaction.commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home: {
                    setTabStateFragment(TabState.HOME);
                    return true;
                }
                case R.id.trending: {
                    setTabStateFragment(TabState.TRENDING);
                    return true;
                }
                case R.id.upload: {
                    setTabStateFragment(TabState.UPLOAD);
                    return true;
                }
            }
            return false;
        }
    };

    enum TabState {
        HOME,
        TRENDING,
        UPLOAD,
    }

}

Upvotes: 4

Related Questions