I don't know how to express this, but the idea was I have a Navigation Drawer that will call (Intent an Activity) and that Activity will automatically replace the content base from the NavigationView link click.
So from my MainActivity -> ReadActivity (Activity call other Activity) and replace the ReadActivity content with the topic click from the NavigationView .
This code I use for replacing:
LayeringViewer layeringViewer = new LayeringViewer();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.layer_frame_layout,layeringViewer);
ft.commit();
I know how to place a fragment. But how can I do this while calling other Activity? Passing the Fragments like Bundles.
if you want to change the content according to selection from drawer, then pass data on drawer close to activity and according to that replace fragments
Ex:
Fragment fragment = new SupportFragment();
Bundle args = new Bundle();
args.putInt(SupportFragment.ARG_NAME, name);
args.putString("screenfrom", screenfrom);
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
You cannot pass fragments in bundles, you can however send extras with your intent and in the receiving activity get the intent and replace the fragment container with a fragment based on the value of the intent.
Related
So I have Activity A with Fragment A.1, and I also have Activity B with Fragment B.1.
What I want to ask is, how do I move directly from Fragment A.1 to Fragment B.1?
I know to move from Fragment A.1 to Activity B, is by:
Intent i = new Intent (getActivity (), MainActivity.class);
startActivity (i);
getActivity ().finish();
But how to move straight to Fragment B.1?
Each Activity A and Activity B has a different <FrameLayout> for Fragment replacement
UPDATE 1.0
I've tried my own way and also the way #cewaphi answered with code like this,
In Activity A:
Intent i = new Intent(TransactionDone.this, MainActivity.class);
i.putExtra("immediatelyTransactionToFragment", true);
startActivity(i);
finishAffinity();
In Activity B:
boolean shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment", true);
if (shouldTransitionToFragment) {
Fragment fragment = new Wallet();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
Log.d("DEBUGGING REDIRECT", "Go to Fragment B.1");
}
The log "Go to Fragment B.1" was created but the transaction doesn't work
When using a single activity and e.g. using the navigation component is not an option.
Consider the following:
In your fragment A.1 when starting the activity store a Boolean
i.putExtra("immediatelyTransationToFragment", true);
In activity B
shouldTransitionToFragment = getIntent().getBooleanExtra("immediatelyTransationToFragment");
// after activity was created
if (shouldTransitionToFragment) {
// Execute the transition action as you would when pressing the button
}
Update 2020/09/07
You are trying to transition to a Fragment from your Activity like this:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainFrameLayout, fragment);
transaction.commit();
The documentation states that you should first add the fragment to the activity:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Apparently your transition works when you click your button. Are you doing it the same?
But I assume at that time the activity has already been created.
Try once to add your fragment instead of replace. I don't know how your container is initialised but adding might be the operation you want, I refer to this good answer for clarification.
Also consider to perform to call this transaction after your activity was created.
I want to send an intent from one activity to a specific fragment like this picture, and fragment is in the fragmentactivity (fragmentactivity includes five fragments). I don't have any idea to implement it.
I don't exactly know what do you mean by "to intent from activity to specific fragment". So I just assume you want to show/navigate to your fragment in your activity. Here's the simple code:
//show your fragment inside your activity
YourFragment fragment = new YourFragment();
getIntent().putExtra("KEY", "Value"); //pass data to your fragment
transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.IDofYourLayoutToBeReplaced, fragment);
transaction.commit();
//in your fragment, retrieve the data
String yourVariable = getActivity().getIntent().getExtras().getString("KEY");
or to better understand, you can check this link
I am using navigation drawer and it is simple to use. I am not providing the complete code but providing you detail which could be easy for you to understand my problem. I am using fragments these are about 8 in numbers and I am replacing them with one an other. But here comes a problem
I am replacing them on click event of the navigation drawer. but there are two main problems
After replacement , I can see the previous fragment in the background. does replace method just call the new fragment over it ? if yes then what should I do to old fragment not be visible in the background of my new fragment.
When I click navigation drawer Item , it loads the specific fragment successfully. but keeping in that fragment when I click to that specific item again it loads this fragment again and again. For example if drawer item num 3 opens fragment MyBook , then by clicking item num three 2 or many times would open fragment that much time.
So please some one answer me how to cure my app for such kind of actions which I described above.
I tried like this. Its working fine me
FragmentManager frgmanager = getFragmentManager();
frgmanager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction frgTransaction = frgmanager.beginTransaction();
if(subitem.equalsIgnoreCase("subitem")){
Frag1 frg1 =new Frag1(mCtx);
frgTransaction.replace(R.id.inflate_layout, frg1);
}else if(subitem1.equalsIgnoreCase("subitem1")){
Frag2 frg2 =new Frag2(mCtx);
frgTransaction.replace(R.id.inflate_layout, frg2);
}else{
Frag2 frg3 =new Frag3(mCtx);
frgTransaction.replace(R.id.inflate_layout, frg3);
}
frgTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
frgTransaction.commit();
you can use addtobackstack in fragmentstranstion object.like
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, new AnotherFragment());
transaction.addtoBackStack(null).commit();
Use replace-method of FragmentTransaction instead of add (http://developer.android.com/guide/components/fragments.html#Transactions)
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, new AnotherFragment());
transaction.commit();
To avoid re-instantiating the fragment, keep track of the current open fragment and only do a fragment transaction, if we next-to-be-opened fragment is a different one than the current.
This may achieved like the following:
class MyActivity ... {
private String currentFragment;
private void openNewFragment(Fragment fragment) {
String newFragment = fragment.getClass().getSimpleName();
if (newFragment.equals(currentFragment)){
// new fragment already shown
return;
}
// Fragment transaction etc here:
}
}
Note that this only compares fragments based in their class name. Sometimes this might not be unique, e.g. if there is a DetailFragment class which displays information about an entity. Which entities details to show may depend on intent arguments.
The above code however will then prevent opening DetailFragment for Entity=1 if currently details for Entity=2 are shown. For these scenarios the information about the fragment kept needs to be extended (e.g. storing a Reference or WeakReference to the fragment instance itself).
i have a Main Activity( activity A ) which has a frame layout that shows 3 fragments ie fragment A,fragment B and fragment C. i have another activity which has a button. When i click that button i want it to open fragment B. how do i open a fragment that belongs to another activity. i have tried the following code
android.app.Fragment fragment = new ImageGridFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
but this seems to work only if you change fragments from the main parent fragment. how can i overcome this and show another fragment belonging top another activity?
You need to pass to your MainActivity some data to let it know that it has to open FragmentB. For example use this in your secondActivity
Intent openFragmentBIntent = new Intent(this, MainActivity.class);
openFragmentBIntent.putExtra(OPEN_FRAGMENT_B, SOME_VALUE);
startActivity(openFragmentBIntent);
and in your MainActivity onCreate:
if (getIntent().hasExtra(OPEN_FRAGMENT_B)
{
Fragment fragment = new ImageGridFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
You need to open your activity A and pass a parameter (for example via extras) so that you are able to launch / open the correct fragment.
#Override
protected void onStart()
{
super.onStart();
if (getIntent() != null && getIntent().hasExtra("something")) {
}
}
I know we can move between activities via an intent call. But is there a way to move from one activity to a particular fragment hosted by an activity? For example if I have an activity A which hosts 2 fragments f1 and f2,is there a way to move from another activity say B to fragment f2 directly via intent call?
Thanks in advance.
hmm it is not possible directly via intent call, you will have to start activity A and via an extra/bundle in intent you can specify to activity A to open fragment f2, meaning you will have to write the logic yourself... not possible directly .. Cheers
Try this in Activity B it will open ActivityA and its sending a string which you can check in that activity, based on that string you will add fragment:
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 toOpen string and open appropriate fragment..
Are you asking how to show/add fragments as well , because thats a complete new ball game :) but this would be a nice starts for managing fragments http://developer.android.com/guide/components/fragments.html
When u open a fragment from a activity on a button click the
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 an id is used: container. This is the id of layout in which Fragment is open.
if u also send a value from activity to with the fragment then
Bundle bundle = new Bundle();
bundle.putInt("value",value );
and
myfragment.setArguments(bundle);
this line is add after create the FragmentManager object
This code is very helpful