Start a fragment via Intent within a Fragment

前端 未结 4 1443
旧巷少年郎
旧巷少年郎 2020-11-29 03:10

I want to launch a new fragment to view some data. Currently, I have a main activity that has a bunch of actionbar tabs, each of which is a fragment. So, within a tab fragme

4条回答
  •  余生分开走
    2020-11-29 03:35

    The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour...

    Creating a new Activity really defeats the whole purpose to use fragments anyway...very counter productive.

    @Override
    public void onClick(View v) {
        // Create new fragment and transaction
        Fragment newFragment = new chartsFragment(); 
        // consider using Java coding conventions (upper first char class names!!!)
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
    
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);
    
        // Commit the transaction
        transaction.commit(); 
    }
    

    http://developer.android.com/guide/components/fragments.html#Transactions

提交回复
热议问题