简体   繁体   中英

Finishing an activity brings me back to my first activity

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM