简体   繁体   中英

Android: Switching from 2nd activity back to 1st quits both

The solution to this problem was pretty simple:

Unfortunatly i somehow commented this wrong in my code, maybe i copied it from somewhere:

public void onUserLeaveHint() { // this only executes when Home is selected.
        if(started){
            started=false;
            recordTask.cancel(true);
            }
            this.finish();
            super.onUserLeaveHint();
        }
    }
}

Thats from the API:

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.

So whenever a new activity is started, the main went in background and was finished.

Thanks for help to all anyway. Stupid fault of mine, simply forgot about that method. Problem solved.

Original Question:

I reduced the code of the second activity to only give a result back for now, it looks like this. (The second activity works when i uncomment the whole code. Its just a file explorer, which should give String path back. For debugging i use this untill i can get it to work.) Both activities are declared in the manifest.

public class AndroidExplorer extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.olddata);

        Intent sender=getIntent();
        Intent intent=new Intent();
        intent.putExtra("ComingFrom", "Hello");
        setResult(Activity.RESULT_OK, intent);
        AndroidExplorer.this.finish();
    }
}

This is called from the first activity by:

Intent intent;
intent = new Intent(firstactivity.this,AndroidExplorer.class);
firstactivity.this.startActivityForResult(intent,0);

And the result should be recieved by:

@Override
    public void onActivityResult(int requestCode,int resultCode,Intent data)
    {
     super.onActivityResult(requestCode, resultCode, data);

     String extraData=data.getStringExtra("ComingFrom");
     Log.e("result", extraData);
     go2startscreen();
    }

Unfortunatly it wont return to the first activity, it just closes the app.

Error Log:

04-26 11:11:14.096: D/memalloc(32383): /dev/pmem: Mapped buffer base:0x51b3e000 size:17645568 offset:15556608 fd:53
04-26 11:11:32.264: D/memalloc(32383): /dev/pmem: Mapped buffer base:0x52e66000 size:3686400 offset:1597440 fd:59
04-26 11:11:32.584: D/memalloc(32383): /dev/pmem: Unmapping buffer base:0x51b3e000 size:17645568 offset:15556608
04-26 11:11:32.584: D/memalloc(32383): /dev/pmem: Unmapping buffer base:0x52e66000 size:3686400 offset:1597440
04-26 11:11:32.644: W/IInputConnectionWrapper(32383): showStatusIcon on inactive InputConnection
04-26 11:11:32.644: W/IInputConnectionWrapper(32383): InputConnection = android.view.inputmethod.BaseInputConnection@40d9ddd0, active client = false

And here is the go2startscreen function:

void go2startscreen(){
        setContentView(R.layout.startscreen);
        appPosition = "startscreen";
        newRecord = (Button) this.findViewById(R.id.newRecord);
        newRecord.setOnClickListener(this);
}

So it should end at R.layout.startscreen. Calling go2startscreen works, when i call it from the first activity.

Check resultCode if its ok then do what u want--like this::

@Override
   public void onActivityResult(int requestCode,int resultCode,Intent data)
   {
    super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
    String extraData=data.getStringExtra("ComingFrom");
    Log.e("result", extraData);
   go2startscreen();
    }
  }

Hope it will help you..

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