In my Android project, I have 2 Activities. One is MyProject and the other is MyProjectOutput.
In MyProject, there is a button(btnProcess) and it would do some processing and starts the second activity using the following code:
btnProcess.setEnabled(false);
// does some network access in an AsyncTask and fetches some stuff, and store some data in the variable 'my_extra_content'
//...
Intent i = new Intent(this, MyProjectOutput.class);
i.putExtra("extracontent", my_extra_content);
startActivityForResult(i,1);
In the second activity, it has a Button to go back to the main activity(a software back button). And the code is:
final Button btnBack = (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//when Back button is pressed, it is cancelled
setResult(Activity.RESULT_CANCELED);
finish();
}
});
And in the main activity, I am using this to re-enable the button:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
btnProcess.setEnabled(true);
}
My program works fine in the emulator(AVD). And I have given this to one of my friend who owns an Android device. And after testing, he said that the works fine but when the back button(hardware button) is pressed from the main activity, the program is not terminating. Still in memory ! But when I tested in the AVD, when I press the hardware back button from the main activity, it would show the home screen of the phone !
I am using a Nokia device(5230 to be exact). And I know that when we press the “end” button, some programs may still reside in the memory which I have to manually close it via the task manager.
So, I am a bit confused at this problem ! Are there anything that I have to taken care of, while using two activities ?
It’s not supposed to terminate. There have been many many articles and questions on this. Android itself decides when to remove an activity from memory. This is based on hardware specs on the phone, memory utilization, and other factors. So pressing End/Back might or might not leave the application in memory. Using a task killer on Android while popular, is a very bad idea.