Finish () does not close activity when called after the first run

I have three actions

  • MessagesAttachPhotoActivity
  • MessageGalleryFolderSelectorActivity
  • ImagePickerActivity

MessagesAttachPhotoActivity calls MessageGalleryFolderSelectorActivity using startActivityForResult() .

Operation

MessageGalleryFolderSelectorActivity displays the photo folders on the phone, and one selects the folder.

ImagePickerActivity then called using setActivityForResult() . When an image is selected from ImagePickerActivity , it returns to MessagesAttachPhotoActivity through MessageGalleryFolderSelectorActivity .

When I launch the application for the first time, everything works fine. However, if I try to select the image again, MessageGalleryFolderSelectorActivity will not close after setResult() .

I tried calling finish() , this.finish() , ((Activity)getApplicationContext()).finish() and super.onBackPressed() without success.

Why is activity not closing during successive launches?

Here is my code:

Call MessageGalleryFolderSelectorActivity:

 Intent intent; Bundle arguments = new Bundle(); Bundle bundle; intent = new Intent(this, MessageGalleryFolderSelectorActivity.class); bundle = new Bundle(); bundle.putInt(Constants.INTENT_EXTRA_LIMIT, Constants.IMAGES_SELECT_LIMIT); bundle.putInt("Request", MessageThread.MessageType.IMAGE); intent.putExtras(bundle); startActivityForResult(intent, MessageThread.MessageType.IMAGE); 

ImagePickerActivity:

 imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_MODE, ImagePickerActivity.MODE_MULTIPLE); imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_LIMIT, 10); imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_SHOW_CAMERA, false); imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM,album); //imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, images); startActivityForResult(imagesIntent, MessageThread.MessageType.IMAGE); 

Passing data back to MessageGalleryFolderSelectorActivity:

 Intent data = new Intent(); data.putParcelableArrayListExtra (ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, selectedImages); data.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM,album); setResult(RESULT_OK, data); finish(); return true; 

Trying to pass data back to the original call activity, but this action does not close MessageGalleryFolderSelectorActivity:

 @Override protected void onActivityResult(int requestCode, int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { ArrayList<Image> selectedImages = data.getParcelableArrayListExtra (ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES); String album = data.getStringExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM); Intent intent = new Intent(); intent.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM, album); intent.putParcelableArrayListExtra (ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, selectedImages); setResult(Activity.RESULT_OK, intent); this.finish(); return; } else if (resultCode == RESULT_CANCELED) { } } 
+7
android android-intent android-activity startactivityforresult
source share
4 answers

Try using finishAffinity() instead of finish() in action.

+1
source share

I think we can reference parent activity using getParent (). So, in the MessageGalleryFolderSelectorActivity class, we can write ((Activity) getParent ()). OnActivityResult (requestCode, resultCode, data) on an overridden onActivityResult. So, we pass the obtained values ​​to the parents without processing it.

0
source share

It works great. I checked the same thing with 3 activities: 1. MainActivity 2. SecondActivity 3. ThirdActivity

In MainActivity, I started SecondActivity by pressing a button, the code is as follows:

 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { nextclick(); } }); } public void nextclick() { Intent intent=new Intent(MainActivity.this, SecondAcivity.class); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 1: break; } } 

In SecondActivity, I started ThirdActivity by clicking on floatActionButton, the code is as follows:

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { nextclick(); } }); } public void nextclick() { Intent intent = new Intent(SecondAcivity.this, ThirdActivity.class); startActivityForResult(intent, 2); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case 2: //Set data for MainActivity Intent intent = new Intent(); intent.putExtra("album", "dfdfd"); setResult(RESULT_OK, intent); SecondActivity.this.finish(); break; } } } 

In ThirdActivity, I started ThirdActivity by clicking on floatActionButton, the code is as follows:

 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Set data for SecondActivity Intent data = new Intent(); data.putExtra("album","album"); setResult(RESULT_OK, data); finish(); } }); } 

Hope this helps you find the exact problem in your code.

0
source share

I tried your logic with three actions like

Activity A

  public class A extends AppCompatActivity { private static final int BCODE = 100; private String Tag="A Activity"; Button triggerButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); triggerButton= (Button) findViewById(R.id.triggerButton); triggerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent; Bundle arguments = new Bundle(); intent = new Intent(A.this, B.class); intent.putExtras(arguments); startActivityForResult(intent, BCODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==BCODE){ if(resultCode==RESULT_OK){ Log.e(Tag,"succes"); } } } } 

From activity A, I started activity B, skipping intention with startActivityForResult

From activity B, I do the same thing again

 public class B extends AppCompatActivity { private static final int CCODE =200 ; private String Tag="Activity B"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); Intent intent; Bundle arguments = new Bundle(); intent = new Intent(this, C.class); intent.putExtras(arguments); startActivityForResult(intent, CCODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode==CCODE){ if(resultCode==RESULT_OK){ Log.e(Tag,"suceess"); setResult(RESULT_OK,new Intent()); finish(); } } } } 

In step C, I just end the operation after setting the result

 public class C extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_c); setResult(RESULT_OK,new Intent()); finish(); } } 

When activity C ends with os, action B resumes and onActivityResult () is also called. In onActivityResult of Activity B, I set the result and ending acitivity.Then OS will resume action B and call onActivity the result of the activity AI tried this many times, this scenerio works fine for me.

-one
source share

All Articles