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 intent=new 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();
}
});
}
}
Maybe the code in your Activities has a call to
finish()inonStop()oronPause(). 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.
There are calls to
finishandstartActivityin there, although I cannot see why they should be executed. I’d put aLogstatement by each. Then try it again and see what the Logcat output says when you minimise and relaunch the app.