I Tried the following code to move from a Fragment extends Fragment to an Activity extends activity.
Intent mIntent = new Intent(getActivity(), UserProfile.class);
Bundle bundle = new Bundle();
bundle.putBoolean(Constant.isFromChatScreen, false);
mIntent.putExtras(bundle);
startActivity(mIntent);
In BaseActivity.java I have Fragment A, Fragment B, Fragment C..from Fragment A I moved to Userprofile.java now I want to move back to Fragment A onBackPressed
How can I do that?
Check Google documentation for findFragmentByTag() here.
You can also use addToBackStack() while calling the fragment from FragmentTransaction
Just use addToBackStack(null) before commit when you make fragment transactions and then when you press the back button you will get back to your fragment.
Old question, but anyone looking for an answer to this can just do this:
#Override
public void onBackPressed() {
finish();
}
finish() "closes" the current Activity and takes you back to the Fragment (Actually, it takes you back to the Activity hosting the Fragment)
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 have navigation bar on my app. Using that has created fragments. I added regular activities to the app and I have found how to go from a Fragment page to an activity page with using the onclick method.
By the click of a button I want to go from an activity page to a fragment page. How do I go about that?
Thank you
apply on click listner on the button and apply below code :-
Fragment fragment = new VehiclesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
If you want to close the current activity to go back to your Fragment-container Activity you may call the finish() method or onBackPressed() method (in case you're not overriding it).
If you want to bring a fragment to the current activity you must have a fragment container and attach it with:
getSupportFragmentManager().beginTransaction.add(R.id.container, fragment, TAG).addToBackStack().commit();
Try this code..
btn.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),FragmentTest.class);
getActivity().startActivity(intent);
}
});
If you want to move around in fragment then you have to be using either navigation bar clicks on tabs or use this from one fragment to another on a click of button .. like this
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = Fragment.instantiate(MainActivity.this, <name of the fragment>);
Bundle bundle = new Bundle();
bundle.putBoolean(ALL_EVENTS, true);
fragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Simply use this:
Intent(view.context, DestinationActivity::class.java).also {
view.context.startActivity(it)
}
There are similar questions, but none of the are solving my issue.
My app flow is following:
Activity home starts Activity B(which does the setup work)
In activity B hosts three screens as fragments...
Fragment1 -> fragment2-> fragment 3.
This is how I make fragments. I am not using replace.just adding it.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = null;
if (getFragType().equals("simple")) {
fragment = new SimpleFragment().newInstance();
}
if (getFragType.equals("inter")) {
if (getFragType.getComponent().equals("con"))
fragment = new SetupConFragment().newInstance();
else if (getFragType.getComponent().equals("ben"))
fragment = new SetupBenageFragment().newInstance();
}
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
Fragment3 has a done button. So once all the 3 steps are done from fragment 3 am launching a new activity C.
The issue is:--
From the activity C , if the user presses the back button, it diplays fragment 2, then fragment 1. Ideally once the user is in Activity C, back press should just exit the screen
I have used all combinations of the intent flags, but it is not helping.
And this is on my button click.
Intent launch = new Intent(mContext, MainActivity.class);
launch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launch.putExtra("Exit me", true);
mContext.startActivity(launch);
((ConfigureActivity)mContext).finish();
Any help will be appreciated.
You can do this way:
Clear Fragment stack:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Clear Activity stack:
Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Hope this will help you.
Say, if you want to start Activity B in Activity A but you don't want user to go back to Activity A,
final Intent intent = new Intent(this, ActivityB.class);
startActivity();
finish(); // kill the current activity (i.e., Activity A) so user cannot go back
In your Activity C, you can control Back key by overriding onBackPressed().
#Override
public void onBackPressed()
{
// code here depending on your needs
}
I have this design problem. I have an activity which hosts two fragment and any given point of time only one activity is visible.
Activity A hosts Fragment B and Fragment C
Host Activity A implements FragmentCommunicator interface and implement respond(int code) method using this method communicator both Fragment B and C can talk to host Activity.
Now here is the problem.
In onClick of Host activity I check certain condition and based on that I take decision which fragment to show.
public void onClick(View v) {
switch (v.getId()) {
case R.id.some_button
if(authNotedone)
showFragmentA();
else{
EDIT: //setting some properties before showing Fragment B
showFragmentB();
}
}
}
So far it works fine. If condition is true FragmentA will be visible with login form. After successful login I would like to show fragment B again. How can I achieve this.
What I have tried?
1) After successful login Fragment A send message to Host activity using Fragmentcommunicator's respond(code) method but it was ugly design as I have to either call performClick() or call showFragmentA() in respond method if code is success.
There could be multiple such conditions in my program How I can handle these neatly?
Use a interface as a call back to the activity. Once you get the message in the activity there is no need to click the button just replace the existing fragment in the container.
Implement the interface in the activity
FragmentB newFragment = new FragmentB();
Bundle args = new Bundle();
args.putInt("key", "message");
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
// replace with fragmentb. no need to perform click again.
// based on the message you decide which fragment you want to replace with
transaction.commit();
You can find an example #
http://developer.android.com/training/basics/fragments/communicating.html
Do you want to navigate between fragments? If so, why don't you implement navigation tabs, have a look at this link.
I have this issue in Android. Consider three Fragments: A, B, C. C can be called from A or from B. Is there anyway to know from which fragment, C was called?
Edited
Ok guys I'm going to explain what I'm trying to do. Suppose I have this call: A calls B, B calls C.
When I press the back button in C It gets me to B, thats fine. But when I press the back button again, it takes me to C, instead of A.
This is my code:
#Override
public void onBackPressed() {
//this is the current fragment
Fragment fragmentActual = this.getSupportFragmentManager().findFragmentById(android.R.id.tabcontent);
String fragmentTag = fragmentoActual.getTag().toString();
//If I press the back button in C:
if(fragmentTag.equals("TAG C")){
Fragment removefragment = this.getSupportFragmentManager().findFragmentByTag("TAG B");
Fragment fragmentClient = this.getSupportFragmentManager().findFragmentByTag("TAG C");
//If Im NOT passing arguments to B, I know this is a new form
if(removefragment.getArguments()== null){
//I always pass arguments to fragment C, but just in case:
if(fragmentClient.getArguments()!= null){
Bundle mArguments = fragmentClient.getArguments();
//FRAGMENT B
FragmentB fragmentB = new FragmentB ();
fragment.setArguments(mArguments);
FragmentManager manager = this.getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.addToBackStack(null);
ft.replace(android.R.id.tabcontent,fragmentB,"TAG B");
ft.commit();
}
}else{
super.onBackPressed();
}
}else{
super.onBackPressed();
}
}
Now I'm going to explain the code. Basically what it does is to replace fragment B, when fragment C is called. I do this, because I need to pass arguments to fragment B. But when I do the replacement, the "history" of the fragment B is lost, I mean when I press back button in B, I cant go back to fragment A (HERE IS WHY I WANTED TO KNOW IF I CAN KNOW WHO CALLS WHOM).
The firts time when I call fragment B, I dont pass arguments because is a blank form. But when I call to C, staying in B, I need to pass arguments to fragment B (when back button is pressed), so It can shows updated information.
Please if there something that is not clear, let me know so I can explain myself better.
Edited 2: This issue has something with this https://stackoverflow.com/questions/16703604/back-press-button-when-i-save-a-form-for-the-first-time-a-list-view-is-not-updat. Maybe it does my idea more clear.
The answer of Luksprog I think is the best: "You may want to tackle those issues. You always have the option of passing a Bundle with data to the newly created fragment mentioning who called it. Then using getArgument() in the fragment will know who called it.".
I haven't found another better way.
You can use the setTargetFragment method to set which was the parent fragment. Then you can use the method getTargetFragment to find out who called you.
What you are doing is transitioning between Fragments, call addToBackStack() as part of your FragmentTransaction:
i guess, This is what you need.
private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
private void showFragment() {
final Myfragment fragment = new MyFragment();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
transaction.addToBackStack(null);
transaction.commit();
}
#Override
public void onBackPressed() {
final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
super.onBackPressed();
}
}
You can use FragmentManager for creating a back stack of your fragments. You also have to work with Fragment Transactions first. Further informations see: http://developer.android.com/guide/components/fragments.html#Transactions