I have a problem concerning startActivity(intent) and onStop. The Android API suggests to save data in onStop, which is what I am doing here:
public void onStop(){
super.onStop();
if(tosave)
{
Editor editor = sp.edit();
editor.putInt(getString(R.string.index_of_text_color), text_color_index);
editor.putInt(getString(R.string.index_of_background_color), background_color_index);
editor.commit();
}
}
However, I would like to start the next activity once it is saved- so I need to use an intent and startActivity(intent).
public void click(View v){
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
My question is, does startActivity(intent) with the two flags call the onStop() method as it is finishing the application, or do I need to call finish()? Is it allowed for me to call finish() after I start a new activity? Or, is it because the new activity will be at the top, Android automatically calls onStop as it is in the background now?
There are so many questions about how startActivity(intent) works with the app cycle that I do not understand. It will be helpful if someone points me to a link.
Thank you!
EDIT: the intent is not in onStop. It is in another method which corresponds to a button. As soon as the button is clicked, I will need to save the data-- go to onstop-- and then go to the next activity. How can I do that?
You should save state in onPause(), not onStop(), because under certain conditions onStop() will never be called. You are guaranteed that onPause() will be called.
onPause() will always be called on your activity when another activity is shown in front of it.
That should solve your problem.
Related
Suppose I have two activities A and B activity A which contains a button I want to start Activity B when I press Button without intent.
According the Oficial Documentation:
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
So you have to use it to open activities with no exceptions or workarounds, if you do that, you are ignoring the entire system architecture.
There is no way to start an activity from anotherone without an intent.
If the reason of not using Intent that you don't want the the user to re-enter the previous activity
You can use finish() to finish that activity intent after you done work with
if(currentUser == null){
startActivity(new Intent(MainActivity.this,StartActivity.class));
finish();
}
So user will be unable to back again
If you want to do some code while the activity is finishing
You can use onDestroy() override method, Sometimes it can also be called if the activity is being killed by the android itself so you can add
isFinishing() function
Inside onDestroy() method which checks whether the application is closing by the call finish() returning true or otherwise by anything else returning false then you can easily specify your code for each situation.
#Override
protected void onDestroy() {
super.onDestroy();
if(isFinishing()){
// Activity is being destroyed by the function `finish()`
// What to do...
}else{
// Activity is being destroyed anonymously without `finish()`
// What to do...
}
}
Put your activity inside a Fragment and start the fragment fromo the button.
These are the possible ways to start any Activity
1st
startActivity(new Intent(Activity_A.this, Activity_B.class));
2nd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivity(intent);
3rd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivityForResult(intent,code);
I Start a second activity when my first activity is paused.
FirstActivity.java
#Override
public void onPause(){
super.onPause();
startActivity(new Intent(this, SecondActivity.class));
}
When I press on the homescreen button, SecondActivity will start but with a delay. In this delay, there is enough time to open a new app (like messenger for instance). However, when I open a new app, SecondActivity will not start anymore (it won't even call the onCreate method of SecondActivity).
How can i still start SecondActivity even when I open a new app?
Override the onBackPressed() method and start new activity from there, instead of adding your code into onPause().
#Override
public void onBackPressed()
{
startActivity(new Intent(this, SecondActivity.class));
super.onBackPressed();
}
I think I have a solution, if you would like to launch the new activity I would go with onStop, instead of onPause.
Start the new activity with the flag of: singleInstance
<activity ..
android:launchMode= "singleInstance" />
Please use onStop() in the second activity and call finish() there. so you killing the 2nd activity instance, when leaving the app. You can restart it easily.
I have an Activity and i want to close it without calling onDestroy(). So I use onStop(); which looks like
#Override
public void onStop(){
super.onStop();
try{
unbindService(mServerConn);
}
catch(Exception e){
e.printStackTrace();
}
}
I runs the code but the activity is still visible on the screen
Can you help me to hide it?
Thanks!
Usage of onStop() is not correct in that situation. If you need to close activity use finish().
Intent openStartingPoint = new Intent(this, MainActivity.class);
openStartingPoint.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(openStartingPoint);
finish();
This should work fine.
You shouldn't call onStop() but you can go about like this:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
This will call onStop() and lead you to the home screen.
onStop() will be called when your activity is not visible on
foreground to user.
So, if your activity is currently visible to user, then it means
onStop() is yet to be called from Android Framework
Your approach is correct. You are unbinding from service in onStop() which is right way.
I think you application will run correct and there is no need of modification
Thanks
use System.exit(0); to close it without calling onDestroy().
If I call an activity Activity_B and 1 of it's instances is already present in the backstack, I want to go back to that Instance, removing all the activities in between, and recreate that instance(to refresh content).
I tried:
Intent intent = new Intent(Activity_A.this, Activity_B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
and in Activity_B:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d("Activity_B", "onNewIntent");
//Thought of refreshing the content here.
}
I never found anything which will do all the above actions.
The best I could found was FLAG_ACTIVITY_SINGLE_TOP which will not recreate the Activty, instead it will call onNewIntent() where I can refresh the content.
But every time a new instance of the Activity_B is called.
What am I missing?
Thank You
Try using FLAG_ACTIVITY_REORDER_TO_FRONT instead. It will bring the previously running instance of your activity to the front if it exists.
Once the activity comes to the foreground, it's onResume() method will be called as usual where you can refresh your UI.
I know this has been asked. I browsed and tried whatever I have found but for some reason, it is not working for me.
my code is as follow
runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(LoginActivity.this, HomepageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I can not use
android:noHistory="true"
as my activity in some circumstances has to stay on the stack.
So, i have the following behaviour. I log in into my app with the LoginActivity, then it goes to the HomepageActivity, but if I press the Back bottom, the LoginActivity pops back again, which I do not want.
Any idea how I could fix this.
This can be done by calling finish() right after startActivity().
finish() destroys the current Activity and therefore removes it from the Stack.
Call finish() right before startActivity().
You can override onBackPressed method of your activity to do what you want