Reputation: 2948
i have a fragment say F1
and from there i'm opening an activity say A1
and after that activity (A1
) i'm opening other activity (A2
) and i'm performing some tasks in A2
now once my task is over in my activity (A2
) i want to destroy current opened Activities and move back to my fragment F1
.
this is what i'm trying to get back to my fragment:
Intent intent = new Intent(context, myFragment.class);
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
but i'm getting this error :
Unable to find explicit activity class
and i know why i'm getting this (cause specified class is not an activity that is a Fragment ) but my question is how can i move back to that fragment ?
Upvotes: 3
Views: 10213
Reputation: 81
Put below code in your Activity from which you want to move.
btnDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SelectLocation.this,MainActivity.class);
intent .putExtra("tag",true);
finish();
startActivity(intent);
}
});
and put below code inside oncreate methodin your activity in which you have attatched all fragments.
public void onCreate(Bundle savedInstanceState){
Bundle extras = getIntent().getExtras();
if(extras!=null && extras.containsKey("flag"))
boolean flag= extras.getBoolean("flag");
if(flag){
currentFragment = getOneFragment();
}
}
this will work properly for you .
Upvotes: 0
Reputation: 9187
You cannot start a Fragment with intent. your every fragment will be attached to some activity. Fragments can be replaced or added in activity,
What you can do is : Your fragment F1 is attached to some activity...say A0. So what you can do is like this :
Intent intent = new Intent(context, A0.class);
intent .putExtra("openF2",true)
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(intent);
then, inside your activity A0's onCreate you can check for which fragment to replace:
public void onCreate(Bundle savedInstanceState){
Bundle extras = getIntent().getExtras();
if(extras!=null && extras.containsKey("openF2"))
boolean openF2 = extras.getBoolean("openF2");
if(openF2){
//add or replace fragment F2 in container
}
}
This is just pseudo code. I hope you will understand it.
Edit: If you want to move back to that previous fragment, you can explicitly call activity's onBackPress()
twice to go back to your activity say A0
which contains the fragment. Not a good idea though.
Upvotes: 2