开发者

Call Main Activity using an Intent

开发者 https://www.devze.com 2023-02-15 19:24 出处:网络
Is it possible to recall the main activity from another activity using an intent? I tried running the code below (the main Activity is ImageSelection):

Is it possible to recall the main activity from another activity using an intent? I tried running the code below (the main Activity is ImageSelection):

public void onClick(View v) {
  Intent intent = new Int开发者_JAVA百科ent(this,ImageSelection.class);

  switch(v.getId()) {
    case R.id.button1:
      startActivity(intent);    
  } 
}


Yes, that is the correct way to start the ImageSelection activity.

If you'd like to bring a previous ImageSelection to the front or clear the Activities on top of it, try one of the Intent flags:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

or:

intent.setFLAGS(Intent.FLAG_ACTIVITY_BRING_TO_FRONT);


Try using this code instead of your onClick() class:

Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        Intent i = new Intent(getApplicationContext(), ImageSelection.class);
        startActivity(i);
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消