Displaying a specific snippet with an intentional call

I know that we can navigate between activities through an intent call. But is there a way to go from one activity to a specific chunk hosted in the activity? For example, if I have an activity A that hosts 2 fragments f1 and f2, is there a way to navigate from another activity, tell B to fragment f2 directly via an intent call? Thanks in advance.

+3


source to share


2 answers


hmm is not possible directly through the intent call, you will need to start activity A, and through the additional / package in the intent you can specify the activity A to open the f2 fragment, that is, you will have to write the logic yourself ... not directly .. Cheers Try this in Activity B, it will open ActivityA and send a line that you can check in this activity, based on this line you will add a snippet:

 Intent i = new Intent(this, ActivityA.class);
 i.putExtra("toOpen", "fragment 1");
 startActivity(i);

      

and in ActivityA oncreate



 Bundle extras = getIntent().getExtras();
 String toOpen = extras.getString("toOpen");

      

check to open the line and open the corresponding snippet. You are asking how to show / add fragments because this is a complete new ball game :) but that would be nice for managing fragments http://developer.android.com/guide/components/fragments.html

+4


source


When u open snippet from activity on button press write this code

Fragment myfragment;
myfragment = new Your_Fragment_Name();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.container, myfragment);
fragmentTransaction.commit();

      

In my code uses id: container

. This is the ID of the layout in which the snippet is open.

if u also sends a value from the fragment action, then

Bundle bundle = new Bundle();
bundle.putInt("value",value );

      



and

myfragment.setArguments(bundle);

      

this line is added after the FragmentManager object is created

This code is very helpful

+1


source







All Articles