Reputation: 1568
I have a method. By using this method i am starting one ListActivity. Here is my source code.
private void onCollectionClicked() {
Intent i = new Intent(getBaseContext(), FileDialog.class);
i.putExtra(FileDialog.START_PATH, last_folder);
startActivityForResult(i, PICK_FILE_CODE);
}
In started ListActivity i have cancel button. If i click this button it should come back to the MainActivity. Here is my source code
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setResult(RESULT_CANCELED);
finish();
}
});
My problem is ListActivity got killed, But it is not comming back to MainActivity. How can i solve this problem?
Upvotes: 0
Views: 78
Reputation: 6108
The following example code demonstrates how to trigger and intent with the startActivityForResult() method.
public void onClick(View view) {
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
// Set the request code to any code you like, you can identify the
// callback via this code
startActivityForResult(i, REQUEST_CODE);
}
If you use the startActivityForResult() method then the started activity is called a Sub-Activity.
If the sub-activity is finished it can send data back to its caller via Intent. This is done in the finish() method
@Override
public void finish() {
// Prepare data intent
Intent data = new Intent();
data.putExtra("returnKey1", "Swinging on a star. ");
data.putExtra("returnKey2", "You could be better then you are. ");
// Activity finished ok, return the data
setResult(RESULT_OK, data);
super.finish();
}
Once the sub-activity finishes, the onActivityResult() method in the calling activity is be called.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("returnKey1")) {
Toast.makeText(this, data.getExtras().getString("returnKey1"),
Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 1
Reputation: 1568
Sorry.. This question is wrong. Actually i am killing my MainActivity unknowingly. This is my onPause code.
@Override
public void onPause() {
super.onPause();
//safe close..
android.os.Process.killProcess(android.os.Process.myPid());
}
Thanks @Pankaj kumar. Because of your comment, i have checked my onPause method code.
Upvotes: 0