Android: How to go back previous Activity - android

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()

Related

Navigate from Activity to Fragment

I am working an android app that has one activity and multiple fragment so I added a lockout feature which locks out the user if there is no interaction in ten minutes. I did this by creating an activity that extends AppCompatActivity and navigates to lockScreenActivity if there is no interaction with the app. Now after the app is unlocked from a web socket I want to navigate back to the activity retaining the current fragment before navigation. How should I go about it.
Searches in stack overflow suggests that I do:
startActivity(new Intent(this, MainActivity.class));
This does not work for me, calling finish on LockScreenActivity did not work too.
Thanks.
If you are going from a Fragment to your MainActivity you should do this
Intent intent = new Intent(getActivity(), mFragmentFavorite.class);
startActivity(intent);
But if you are going from Activity to Fragment , do this
Intent intent = new Intent(this, mFragmentFavorite.class);
startActivity(intent);
if you are doing a finish() going to lockActivity and doing another finish inside lockActivity , there is no activityes in the stack to inflate, maybe thats why is not working for you
If you want to go back from Activity to Fragment.
This is very simple just override onBackPressed() in your activity and call onBackPressed where you want. See my code bellow this will more clear your concept.
#Override
public void onBackPressed() {
super.onBackPressed();
}
//now call onBackPressed to navigate from activity to fragment
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});

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();
}

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();
}

Return back to previous previous activity

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.

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);
}

Categories

Resources