Issue
I have an app which starts from a splash screen and then navigates to other activities. If i press the home button in a particular activity ,the app gets minimized. Again if i click on the app icon, the app starts from the splash screen. I want to resume my app from the activity in which the home button was pressed. How to achieve this?
package com.xyz.user.login;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
public class ResetPasswordActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.logout:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
PopIt("Are you sure you want to exit?");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void PopIt(String string) {
AlertDialog.Builder builder new AlertDialog.Builder(this);
builder.setMessage(string)
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent new Intent(
getApplicationContext(),
SignInActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert builder.create();
alert.show();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reset_password);
ImageButton imgRPass(ImageButton)findViewById(R.id.imgChangePass);
imgRPass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intentnew Intent(ResetPasswordActivity.this,ResetPasswordMessageActivity.class);
startActivity(intent);
}
});
ImageButton imgBack(ImageButton)findViewById(R.id.imgbtnBackFromResetPass);
imgBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
}
}
Solution
Maybe the code in your Activities has a call to finish()
in onStop()
or onPause()
. That destroys the Activity when it’s minimised and causes the app to start again. There are other steps to take to be sure of restoring the state, but that is a good place to start looking.
You could try putting this code in there to track what is going on.
private static final String TAG "ResetPasswordActivity";
@Override public void onStart() {
Log.d(TAG, "onStart:");
super.onStart();
}
@Override public void onResume() {
Log.d(TAG, "onResume:");
super.onResume();
}
@Override public void onPause() {
Log.d(TAG, "onPause:");
super.onPause();
}
@Override public void onStop() {
Log.d(TAG, "onStop:");
super.onStop();
}
@Override public void onDestroy() {
Log.d(TAG, "onDestroy:");
super.onDestroy();
}
There are calls to finish
and startActivity
in there, although I cannot see why they should be executed. I’d put a Log
statement by each. Then try it again and see what the Logcat output says when you minimise and relaunch the app.
Answered By – emrys57