0

I'm writing an app that has multiple activities. Activity A calls Activity B, not expecting a result. Then if a button is pressed B startsActivityForResult with Activity C. When Activity C is done, it makes an intent with all of the extras it needs and finishes. The problem is that when it calls this.finish() or just finish(), it brings me all the way back out to Activity A. onActivityResult in Activity B is not called. What is wrong?

Activity A: Starts Activity B

Intent in = new Intent(ccstart.this,mainmenu.class);
in.putExtra("uid",loginresponse);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("usr",text_user.getText().toString());
// Commit the edits!
editor.commit();
startActivity(in);

Activity B: Starts Activity C for result

Intent intent = new Intent(mainmenu.this,filebrowser.class);
startActivityForResult(intent,0);

Activity C: Return statement

Intent result = new Intent();
result.putExtra("fname", file.getAbsolutePath());
this.setResult(Activity.RESULT_OK, result);
finish();

Activity B: Upon the result of activity c...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If the request went well (OK) and the request was PICK_CONTACT_REQUEST
    if (resultCode == Activity.RESULT_OK && requestCode==0) { //upload a file
        final String fname = data.getExtras().getString("fname");
        final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); //Load settings
        final String uid = settings.getString("uid", "");
        new Thread(new Runnable() {

            public void run() {
                // TODO Auto-generated method stub
                doFileUpload(fname, uid);
            }
        }).start();
    }
}

What is the issue with that? It happens with an activity that doesn't return a result as well, so its not just this one.

Thanks!

Osmium USA
  • 1,751
  • 19
  • 37
  • 2
    Post your code for the users to understand the question which in turn will help you. – gsb Jul 06 '12 at 23:04
  • 1
    Without the code, we cannot tell you what you are doing wrong. Please view this document for clarification: http://stackoverflow.com/faq – BlackHatSamurai Jul 06 '12 at 23:30
  • I don't see anything wrong with this code, maybe you are getting some exception and that's causing to go back to home activity? – User Jul 06 '12 at 23:51

1 Answers1

0

You need to explicit close your activity when you start the next one, if not, it stays in the "stack of activities" that you can access with the back button or when the next activity closes.

You need to call finish on activity A after you started activity B

user1494736
  • 2,425
  • 16
  • 8
  • This is wrong, the problem is that C is returning to A directly and not to B. It is expected that A is in the activity stack. But B should be on top of it. – User Jul 06 '12 at 23:48
  • That solved half of the problem. When I press the button to go to Activity C (does it help to mention that it extends ListActivity) it calls onActivityResult before the ui has a chance to render, and when I select something, triggering the finish(), onActivityResult doesn't happen. – Osmium USA Jul 07 '12 at 00:19
  • He's right. [link](http://stackoverflow.com/questions/3354955/onactivityresult-called-prematurely) explains the issue and tells how to fix it in the manifest. Thanks for the help! – Osmium USA Jul 07 '12 at 00:27