简体   繁体   中英

starting activity in onCreate and getting an result

I have an app that opens a config activity at initial start up, and that config activity opens a list activity (startActivityForResult in onCreate). In the list activity the user can choice a value, which closes the list and sends the value to the config activity (setResult, finish, onActivityResult).

That works fine when I try it in the emulator and for almost everyone who has tried it.

However, now someone has send a mail that it does not work on his device (Galaxy): he can select something in the list and close it, but the config activity opens it again (as if it has not received the selected result). What could be going on? What do I need to ask the user to figure it out?

public class AccountInfo extends VideLibriBaseActivity /* extends SherlockActivity */ {
   void openList(){ //called in onCreate and on cancel
        findViewById(R.id.libraryTextView).postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(AccountInfo.this, LibraryList.class);
                //...
                startActivityForResult(intent, REQUEST_LIBRARY_FOR_ACCOUNT_CREATION);
            }
        }, 200); //did not work without delay on some devices
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_LIBRARY_FOR_ACCOUNT_CREATION) {
            if (resultCode == LibraryList.RESULT_OK) {
                libId = data.getStringExtra("libId");
                libName = data.getStringExtra("libName");
                lib.setText(libName);
                //...
            } else if (libId.equals(""))
                if (mode == MODE_ACCOUNT_CREATION_INITIAL && (VideLibriApp.accounts.length == 0))
                    openList();
                else
                    finish();
        }
    }        
}

public class LibraryList extends VideLibriBaseActivity /* extends SherlockActivity */  {
  //...
  lv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
            Intent result = new Intent();
            result.putExtra("libName", localLibs.get(i).get(i2).get("NAME"));
            result.putExtra("libId", localLibs.get(i).get(i2).get("ID"));
            //...
            setResult(RESULT_OK, result);
            LibraryList.this.finish();
            return true;
        }
    });
    //...
}

launchMode is singleTop for both activities

It looks like on some devices due to low memory or other reason AccountInfo might be calling onCreate when the LibraryList was finishedm, You are recalling open list at only two places after getting result from onActivityResult and other is from OnCreate, As you are sure that onActivityResult will always get result

Here are some checks you can try on

  • check you are using startActivityForResult() correctly, do not use startActivity() .
  • if you do something in overriden onBackPressed method, super.onBackPressed(); has to be positioned at the last of the method, not at the first line. (my case to spend 5 hours)
  • remove android:launchMode="singleInstance" in manifest or equivalent argument to create a intent.
  • remove noHistory="true" in manifest of the callee activity.
  • check setResult() is missed.
  • finish() is called to close the activity. use finishActivity() to close callee activity.
  • use requestCode more than zero. negative value does not work.

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