Return back to previous previous activity - android

Here is what the structure looks like:
Activity 1/2/3 -> Activity 4 -> Activity 5
On Act4, if a boolean variable is true, it jumps straight to Act5.
My problem is that when I press back on Act5, i want to get back to the activity which called Act4. But what happens is that it jumps back to Act5. If i use intent on back press, I i wouldn't know which activity called Act4.
in onCreate of Act4, i have this code:
if (boolean) {
Intent intent = new Intent(context, Act5.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
Is it okay to call finish first before the startActivity?
Thanks in advance.

Before calling intent to start act5 from act4 call finish() method.

boolean your_variable = true;
if(your_variable)
{
startActivity(new Intent(Act4.this, Act5.class));
finish();//now this finish() method will finish Act4 so when you press back on Act5 it will return back to Act3
}

My Suggestion is to use startActivityForResult() instead of startActivity(intent) and when you finish from activity5, you can set some flag and can manage while reaching onActivityResult()
if you want to finish activity4 based on flag, do finish() in onActivityResult()

in onCreate method of your Act4 class you can put
if (yourBooleanValue) {
finish();
startActivity(yourIntent);
}
or, it would be better if you would have that boolean in your 3rd activity..

you can manage by passing some value by putExtra by activity 1/2/3 and on activity 5 you can go back to activity according to those values.

Related

How to go previous Activity by using back button

I have created two activity i.e Activity A & Activity B ,if i clicked Next Button i.e in Activity A going to Activity B properly But when i click on back button i want to go from Activity B to Activity A and page swipe from left side to right side and on click next page swipe right to left,
here is my code
public void onBackPressed() {
Intent intent = new Intent(ActivityB.this, Activity.class);
startActivity(intent);
finish();
Just Remove finish() from Activity.
Because when you go to second activity and finish first activity, there is no any activity and stack.
So If you click back button from second activity, application will be finish if there is no Activity in stack.
You should use this Approach.
Ex.
In Activity.java
Intent first = new Intent(Activity.this,ActivityB.class);
startAcivity(first);
// Don't use finish() here.
In ActivityB.Java
Just click on built in back button.
or If you want to use your own back button.
Use finish(); in button click event.
You can use only onBackPressed();
public void onBackPressed() {
super.onBackPressed();
}
Android Overriding onBackPressed()
How to go previous Activity by using back button
No need to put an intent and start a new activity that would take you to previous activity.
Just call 'finish()'
It would go back to previous activity as Android activities are stored in the activity stack
If you have other activities that are present in between the activites say if android stack is filled with Activity A>Activity C>Activity B,If you want to go to Activity A on finish of Activityy B then you have to set an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP
Just use finish() no need for intent as A is already in stack and when you finish B, A will come to surface
public void onBackPressed() {
finish();
}
Read this to learn more about android activity stack.
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
super.onBackPressed();
}

Android: How to go back previous Activity

I have four activities
ListActivity
Itemdetail_Activity
record1_Act
record2_act.
I can go from ListActivity -> record1_Act and from ListActivity <-record1_Act properly but when I want to go from Itemdetail_Activity -> record2_act and return back from Itemdetail_Activity <- record2_act it always goes record2_act to ListActivity.
I even used i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); but it does not work. How to solve this issues?
Here is my code
Intent i = new Intent(record2_act.this, Itemdetail_Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Intent i = new Intent(record1_Act.this, ListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
After starting activity remove finish() call because it will clear that activity from stack and i see you have used finish in your code. also you do not need to set these flags.
In activity 4, implement onBackPressed() as,
#Override
public void onBackPressed()
{
Intent i = new Intent(4.this, 3.class);
startActivity(i);
}
call this method if you have any back button. And if you want to link the backpress for every activity you need to implement onBackPressed() method in every activity.
you can call onBackPressed() if previous activity not finished also you can use :
this.finish();
// Or
Activity currentActivity = this;
currentActivity.finish();
and if previous you finished activity you have to track user in activities and make list of activities, then move to :
activitiList.get(activityList.size() - 2);
// activityList.size() - 1 is last activity and activityList.size() - 2 is previous activity
Do check if ItemDetailsActivity is NOT calling finish()

Finish the activity using component name in android

I am trying to finish one activity from another.
For that purpose I am having only the component name of that activity.
How can i finish that ?
1.Make your activity A in manifest file: launchMode = "singleInstance"
2.When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.
3.When the user clicks modify, call the new Intent or simply finish activity B.
FIRST WAY
In your first activity, declare one Activity object like this,
public static Activity fa;
onCreate()
{
fa = this;
}
now use that object in another Activity to finish first-activity like this,
onCreate()
{
FirstActivity.fa.finish();
}
SECOND WAY
While calling your activity FirstActivity which you want to finish as soon as you move on, You can add flag while calling FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack if you want to show the FirstActivity you will have to call it using intent.
You can do it in very simple way.
First create a static instance of your activity e.g. MainActivity, whom you want to finish like,
public static MainActivity act=MainActivity.this;
and now in another actvity e.g. MainActivity2 just call this line,
MainActivity.act.finish();
Try extending that activity and override the finish method
public class ma extends MainActivity{
#Override
public void finish()
{
super.finish();
}
}
You want to exit application after log out.
that time to user this
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
or try to another way like
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
moveTaskToBack(true);
finish();
}

Navigate to activity by back button doesn't call onstart()

I have an activity(A) and I need to set some text after activity becomes visible to user ,first time I navigate to activity everything is OK,but when I navigate from (A) to activity (B) and press back button ,it returns to (A) button doesn't call onstart of (A).what is the problem?
Back button is navigation to previous activity in activity stack, which is already created and thus its onResume method will be called. So you can do what you want inside onResume().
If it is must for your activity to create new instance do as follows:
If you are on activity A and going B, call A.finish() so it discards A from activity stack, and on B override backPressed and create a new instance of A.
#Override
public void onBackPressed() {
Intent i= new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.finish();
startActivity(i);
}

back to previous activity

I try to go to previous Activity (Activity A) but have problem. Command to this get inside Activity B and I wont to go back to B:
A:
if(...)
{
B.staticF();
}
B:
static void staticF()
{
super.onBackPressed();
}
But I can't use super because it's static context.
Of course, I can call
Intent i = new Intent(this, B.class);
startActivity(i);
but I wont to save B look.
Why not just use something like a shared preference to save the state? and then use the intent to go back and in the onCreate method get the preferences and populate any views with the data you wanted to save

Categories

Resources