How can I resume the previous activity with code? - android

I am using two activities first activity contain some edit texts and the second layout is displayed when the submit button is clicked.
So that the first activity is in pause state and when I press the back button from the second layout the first layout is resumed (That is the previous state is recovered).
Now I need the back button action in my own back button in the screen.
I tried with onFinish() in the button click but the previous state is not resumed.
I tried with startActivityForResult too.
Any other solution for this?
Please share your ideas.
Thanks in advance.

Call the finish() method of your Activity. That will close your second Activity and resume your previous Activity.

You have to make sure that the first activity saves it's state in onPause somehow (e.g. using bundles, database, statics, application class, ...) and then that it sets up the state again from that persisted state in onResume.

Hey try to save the state in
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}

I don't think calling finish(); from arnab answer is the right thing to do. You should put:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
instead of finish(); but above startActivity();

backButton.setOnClickListener(v -> { Intent intent = new Intent(CurrentActivity.this, PreviousActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(intent); }); worked for me finish(); didn't resume the previous activity but started it.

Related

Check if a previous activity exists

I have an activity. After checking some stuff, I want to go back to previous activity if a previous exists, and if not I want to start a SpecificActivity. How do i do it?
Edited:
Seems like no one is understanding what I meant, so let me rephrase. Lets say I am in Activity A. I don't know if A is the only activity in the stack. If there are other activities, then I want to finish A and pop the activity right below A in the stack into the foreground. If A is the only activity in the stack, I want to start some activity Z. How do i do it?
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can simply override the onBackpress()
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Destination_Activity.class));
finish();
}
call the next activity like.
Intent intent = new Intent(this,Your_Next_Activity.class);
startActivity(intent);
then it will call your another activity and your current activity will be on background if you will use finish() after calling next activity it will finish your current activity so don't use finish() after calling your next activity in this scenario.
After that when you press back button it will automatically finish current activity.

How to Quit Android App

I have an Android App, and has several activities. I need to provide the button for each activity or specific activity which should allow to Close the App, without going to back to previous activities or run in background.
I tried
finish();
System.exit(0);
Both combination and individually its not working but closing the current activity and navigate to previous activity.
I looked the code from the following question
Need code for exit from app in android
First, having a Quit button is not reccomended in Android as it's not part of the expected UX.
But if you really need it, then you can call your home activity with an intent containing an extra, for instance :
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra("FORCE_EXIT", true);
Finally in your home activity, when handling the intent (in the onNewIntent() method), if the "ForceExit" extra is set, then finish the activity.
The stack will have been cleared completely by the FLAG_ACTIVITY_CLEAR_TOP and your app will then stop.
The most recommended approach that works for most cases is to feature only 1 Activity, using fragments for content displaying and logic.
This way you only need to finish() the main Activity since it will control the app lifecycle by design.
You will have many other benefits, such as dependency control and reusability, aswell as built-in functionality like animations using fragment transactions while having the possibility of keeping a fragment backstack, which you can manage accordingly towards your expected user interaction and without affecting the conveniency of finishing your app by calling finish() on your host Activity.
Another thing you can do, is to flag intents like this: intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); before launching a new Activity.
This way you can maintain your back trace clean, hence finishing the application whenever the user press the back button or call finish() from any event. However the use of flags is discouraged and considered bad practise.
This may be a hack to solve your problem. but i have just made an app and tested my code and it is working fine.
You will need to create a new activity called QuitActivity or whatever you want to name it and when you want to finish your app or quit your app you will have to start that activity via using this code
Intent i = new Intent(getApplicationContext(), QuitActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
this.finish();
then this is my code for quit activity that does nothing but closes it self after clearing the backstack so your app will quit.
public class QuitActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.finish();
}
}
hope this helps you.
First Finish your current Activity while moving to next by this code
startActivity(intent);
Classname.this.finish();
Second thing just override onBackPressed
#Override
public void onBackPressed() {
//do nothing
}
Use:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
when starting a new Activity, as described in API Documentation.
type only System.exit(0);
or create static boolean needToExit;
set needToExit = true;
in place where you call
finish();
System.exit(0);
in all other activity overwrite onresume
public void onResume(){
super.onResume();
if (MySettingsClass.needToExit){
finish();
System.exit(0);
return;
}
//your other code;
}
I tried some of the answers here using the flags or System.exit(0), but for me it either didn't work or it resulted in weird behavior. Sometimes it would kill the app, but then immediately restart it. I realized that I should just be doing things more of the standard way using request and result codes.
Basically, in your parent activity, start your child activity with:
startActivityForResult(new Intent(this, ChildActivity.class), CHILD_ACTIVITY_REQUEST_CODE);
where CHILD_ACTIVITY_REQUEST_CODE is just a constant (static final) integer. Then in your child activity, when they press the exit button, finish the activity with:
setResultAndFinish(RESULT_CODE_EXIT);
where RESULT_CODE_EXIT is another constant (static final) integer. Then back in your parent activity, handle the result code and finish the parent activity too:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CHILD_ACTIVITY_REQUEST_CODE) {
if(resultCode != ChildActivity.RESULT_CODE_EXIT) {
finish();
return;
}
}
}

Overriding onPause and onDestroy of com.android.settings.* activities

Is it possible to override onPause and onDestroy methods of activity with com.android.settings.LanguageSettings class? All I wanna do is to fire an action when Language&Input setting screen is closed (I mean paused or destroyed). Here's my code so far.
Intent intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.LanguageSettings");
startActivity(intent); // <--- I wanna detect this activity's paused or destroyed
As an alternative way, by starting the activity by startActivityForResult() and overriding onActivityResult(), I could detect user presses the back button and close the activity. However this doesn't work when the home button is pressed and the activity goes to background.
Anyone have any idea?
Why would you want to do that ? You cannot since those activities are not written by you.
If you want to perform some action when the user changes the language at runtime, follow this article:
Handling Runtime Changes
http://developer.android.com/guide/topics/resources/runtime-changes.html
If you need to restore data when the home button is pressed and the activity goes to background you can use onSaveInstanceState() and onRestoreInstanceState(Bundle bundle)
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putLong("myParam", valueOfMyParam);
}

How to clear data from activity using device back button

I had created an android app which has 3 activity (A,B,C) in activity A there is 2 autocomplete textview which fetch data from database and button after selecting data in autocomplete text user press button and it goes to next activity which show listview.
Now the problem is when I press the devices back button from listview is display activity A with selected data in autotext how can I avoid the selected data when I press device back button.
Thanks for helping in advance
When you press the back button, activity A will "resume". So, in your Activity A override "onResume"...in the onResume method, clear your fields.
There is nothing with the listactivity's back button as you are trying to clear you field in the previous activity. As you are not finishing your previous activity while creating the list activity so the previous activity is not changing. So while you are returing from the list you are saying the same data as before. to do so as #Dave suggested you could have done the clearing in the oResume method or onPause method. But there is a problem. If you clear your data in onResume or in onPause you data will be cleared for other pausing or resuming reasons like pressing home, or for other applications etc. So you can do any of two
Option 1:
clear the data of the previous activity when you are starting the list activity
or
Option 2:
instead of startActivity call startActiviyForResult and also override onActiviyResult method. So there you can detect when you are returning from list activity. Then clear the data.
To deal with this problem, I would simply override onBackPressed() method, and in there, I would have cleared activity stack as there is no logical explanation to keep activities in memory, and then start a fresh intent to your original activity.
Here is code.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity_A.class); //I suppose they are in same package
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I hope it helps.
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}
this also works well..
you should implement startActivityforResut
When you come back to this activity on result you can clear the data

Problem in killing activity

I have developed a small android app with 3 activities. In first activity there has a button(Next_Button). If i click the button then the second activity will appear. In second activity there also a button which will forward to third activity after clicking it. In third activity there is a button (Home_Button). If i click this button(Home_Button) then first activity will appear. Now i want to kill second activity when i click the Home_Button in the third activity to make first activity visible. How can i do this?
Please help.
Best wishes
Md. Fazla Rabbi
In third activity, write the below code for your Home button click event:
btnHome.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(this, firstActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
});
For example, for back key pressed, we can override onBackPressed function:
#Override
public void onBackPressed() {
startActivity(new Intent(this, first firstActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return;
}
Update:
For your reference, this one is the same as your requirement: Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP,
And one more example:
Go home feature with FLAG_ACTIVITY_CLEAR_TOP
You don't need to "kill" your activity.
In your third activity, if you start the first one with startActivity, you first activity will be shown.
The system will decide if your second activity (or your third) should be paused or destroyed.
just call finish() method of the second activity on Home button click.

Categories

Resources