Reputation: 5996
At particular point of time in my app, I want to restart the app completely i.e. killing the process associated with the app and then restarting it again. I want to do this to free up heap space as my app contains lot of bitmaps.
I used this SO link with the highest voted answer and created following extra activity as following:
/** This activity shows nothing; instead, it restarts the android process */
public class MagicAppRestart extends Activity {
// Do not forget to add it to AndroidManifest.xml
// <activity android:name="your.package.name.MagicAppRestart"/>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.exit(0);
}
public static void doRestart(Activity anyActivity) {
anyActivity.startActivity(new Intent(anyActivity.getApplicationContext(), MagicAppRestart.class));
}
}
and calling this activity as MagicAppRestart.doRestart(this);
from the required place in another activity.
Now, the issue is this works fine on android 2.3 but on 4.0, this code only exits the app but do not restart it.
Am I doing anything wrong? Is there exist a solution for this issue which will work on all OS?
Also, I have already tried following code, it restarts the but not process and hence does not free up the heap, so it is of no use to me:
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Any help appreciated !
I have 7 activities in my app including home activity. Barring home activity, other 6 activities use ViewFlipper
with 5-8 screens.
I'm using unbindDrawables() in onStop()
and onDestroy()
of every activity but it releases only little portion of the memory.
Hence I'm looking to completely restart the app process.
I just came across this:
In addition, Android changed the way bitmap memory is allocated. Prior to Android 3.0, bitmaps were allocated in native memory, with only a small descriptor kept on the Java heap; now the entire bitmap is allocated from the Java heap. This can lead to the Java heap being used up very quickly if multiple bitmaps are kept in memory.
I guess this is the reason why my approach is working in device with 2.3OS and not on device with 4.0 OS.
Upvotes: 1
Views: 3916
Reputation: 433
Try the top voted answer of Oleg Koshkin in this how to programmatically "restart" android app? . Hope this will help you.
Intent mStartActivity = new Intent(context, StartActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
Upvotes: 0
Reputation: 5996
After suggestions from some people, I concluded it's not ideal/recommended to exit/restart the app through code when the app is running.
So, what I have done is - set a flag to identify home button click i.e.exit of app
At that time, I call android.os.Process.killProcess(android.os.Process.myPid());
to completely kill the process of current app and to avoid running of app in background.
Thanks all !
Upvotes: 0
Reputation: 133560
You should not call System.exit(0)
. It is a bad idea to quit and restart your app.
Quoting Romain Guy from https://groups.google.com/forum/#!topic/android-developers/G_D3pKnGLt0
(Romain Guy): The user doesn't, the system handles this automatically. That's what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a "quit" or "exit" application button. It is useless with Android's application model. This is also contrary to how core applications work.
To know more check the below link
Is quitting an application frowned upon?
You should code in such a way that you don't run into memory leaks.
Managing Bitmap Memory
http://developer.android.com/training/displaying-bitmaps/manage-memory.html
Also check this link on Memory management
https://www.youtube.com/watch?v=_CruQY55HOk
Edit:
You can bind the drawable in onResume
an unbind in onPause
.
Upvotes: 1
Reputation: 888
You can't clear the heap. Read this for better understanding.
Hope it helps.
Upvotes: 1
Reputation: 1493
Try this. It works for me:
Intent it = new Intent();
it.setComponent(new ComponentName(YourActivity.this.getPackageName(),YourActivity.class.getName()));
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_SINGLE_TOP);
YourActivity.this.getApplicationContext().startActivity(it);
Upvotes: 0