How to start a fragment from an action

I already want to run my RecipientFragment from my MainActivity and transfer the data to the Fragment from my MainActivity. Here is the code I implemented. But the fragment does not begin.

Bundle bundle = new Bundle();
            bundle.putString(ParseConstants.KEY_FILE_TYPE, fileType);
            RecipientsFragment keyfile = new RecipientsFragment();
            keyfile.setArguments(bundle);
            Fragment newFragment = new RecipientsFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.commit();

I also want to know how to pass aim.setData and get this data in a Fragment. I currently have this code.

RecipientFragment

mMediaUri = getActivity().getIntent().getData();

Mainactivity

Intent recipientsIntent = new Intent(this, RecipientsFragment.class);
        recipientsIntent.setData(mMediaUri);
+4
source share
4 answers

You can either add or replace a fragment in your activity. Create FrameLayoutin the action layout xml.

Then do this in your activity to add a snippet:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();

And to replace the fragment do the following:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();

Android SO:

add(), replace() addToBackStack()

add() replace()

add() replace()

+17

-, , . -, . - :

FragmentTransaction transaction = getFragmentManager();
    transaction.beginTransaction()
        .replace(R.layout.container, newFragment) //<---replace a view in your layout (id: container) with the newFragment 
        .commit();
+2

RecipientsFragment,

0
source

Easy way

  • Create a new java class

    public class ActivityName extends FragmentActivity {
    @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        if (savedInstanceState == null){
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, new Fragment_name_which_you_wantto_open()).commit();}
     }
    }
    
  • in your activity where you want to call a fragment

     Intent i = new Intent(Currentactivityname.this,ActivityName.class);
     startActivity(i);
    

Another method

  • Place the layout frame in your activity where you want to open the fragment

      <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </FrameLayout>
    
  • Paste this code where u wants to open the fragment

        Fragment mFragment = null;
            mFragment = new Name_of_fragment_which_you_want_to_open();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frameLayout, mFragment).commit();
    
0
source

All Articles