Button to go back to MainActivity

≯℡__Kan透↙ 提交于 2019-11-30 12:36:30

问题


I want to create a button that would lead the user straight back to main activity which doesn't have the android name="com.example.example".
It has android.intent.etc...
How can I reference my button to go back to this activity?


回答1:


Lets say your main activity is called Main.java.

btnBack.setOnClickListener(new OnClickListener(){

  private void onClick(){
    Intent intent = new Intent(currentActivity.this, Main.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);   
    startActivity(intent);
  }
});



回答2:


use startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));




回答3:


Sometimes you can just call activity.finish() to end current activity, so the main (first created) activity will pop out.

If this is not your case, do this:

Intent intent = new Intent(getApplicationContext(), Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)             
startActivity(intent);



回答4:


Intent intent = new Intent(this, Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);



回答5:


Well from wherever you are just call startActivity() with the required parameters inside the buttons onClick method. That's it.




回答6:


public void onBackPressed(){
    finish();
}


来源:https://stackoverflow.com/questions/11460896/button-to-go-back-to-mainactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!