Reputation: 139
In my application I have a lot of activities that jump from one and other by buttons. I want to make sure that when the user clicks on a button to leave that activity, the activity is not running in the background slowing down the application. So I figured the way to solve this is to add onStop
at the bottom of the activity like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homeactivity);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(HomeActivity.this, activity.class);
startActivity(i);
}
});
btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(HomeActivity.this, activity1.class);
startActivity(i);
}
});
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
I need to know if this onStop
is really stopping the activity from running in the background and slowing down the application. If not, can somebody assist me with stopping the activity from running in the background?
Upvotes: 0
Views: 70
Reputation: 15476
Please familiar yourself with Android Activity Lifecycle. The basic premise of Activity is so to structure an app's code in such a way such that Android OS can efficient manage it. For example if the new Activity you started is the game screen that requires lots of memory, Android would destroy all of your app's prior Activities, and reconstruct them when the user exiting the game Activity.
As long as your activities don't do background threading or send frequent messages to handlers, they will not slow down the device in any way. And as mentioned above, since all the Activities not currently in the foreground may be removed by the OS at any time, you shouldn't be doing anything with them in the first place.
Upvotes: 0
Reputation: 7343
After calling startActivity(i);
, call finish();
, that would ensure that your previous activity is killed and won't take up any more resources.
Also, when you override onStop()
, you are not doing anything in it since you're just calling the super
method of that function. In general, activity lifecycle methods are overridden if you want to ensure data continuity. For example, you have an app that takes in user input and you want to preserve it, you override the onPause()
and onResume()
methods to handle these kinds of situations. You do something like take in the input and store it in SharedPreferences
in the onPause()
method, and when the onResume()
method is called, you take those values from the SharedPreferences
and continue using them.
One great example is the onCreate()
override. It is the first function that is called when an activity is initiated. In your onCreate()
, you have your usual call for its super
method, however, you have a lot more going on there. You can actually just have an onCreate()
with only the call to the super
function but that won't do you any good. In the onCreate()
function, you set the layout and assign Button values as well as onClickListeners and other widgets that you have on your layout.
Just keep in mind that when you override an activity lifecycle method, do something in it.
Now, if you want to go back to your previous activity (the one that opened the new one) do this in your second activity:
@Override
protected void onBackPressed(){
super.onBackPressed();
Intent intent = new Intent(this, ActivityOne.class);
startActivity(intent);
finish();
}
That will take you to your first activity and kill the second one.
Upvotes: 3
Reputation: 2927
if you want to stop and kill current activity just call finish(); after you place intent
//start new activity
Intent i = new Intent(HomeActivity.this, activity1.class);
startActivity(i);
//kill current activity
finish();
Upvotes: 2
Reputation: 1194
When you don't need an activity, just after the activity starts call activity method finish(). It will kill the current one after launching an activity.
Upvotes: 0