How to switch between activities

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

In my app I have three activity as

  1. Activity 1

  2. Activity 2

  3. Activity 3

How to switch between activities.

So how can this be done?

 public boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId())      {         case R.id.settingOpt:                                        Intent intent = new Intent(this, SettingsForm.class);             this.startActivity(intent);              this.finish();         break;                   case R.id.reminderOpt:                                       Intent intentR = new Intent(this, ReminderForm.class);             this.startActivity(intentR);            break;          case R.id.helpOpt:                                       Intent intentH = new Intent(this, HelpForm.class);             this.startActivity(intentH);               this.finish();         break;          case R.id.shareOpt:                                          Intent share = new Intent(android.content.Intent.ACTION_SEND);             share.setType("text/plain");             share.putExtra(Intent.EXTRA_SUBJECT, "Name of the thing to share");             share.putExtra(Intent.EXTRA_TEXT, "www.gmail.com");             startActivity(Intent.createChooser(share, "Share LoveCycle's website link with your friends"));         break;          default:          return super.onOptionsItemSelected(item);            }      return true; } 

回答1:

When I click back button from Activity3 I should always went to Activity1,instead of Activity2:

Use following method on back button press event in Activity3:

setResult(R.id.common_backToActivity1); 

And in Activity2 capture that setResult as follows and close this Activity:

protected void onActivityResult(int requestCode, int resultCode,Intent data) {         switch(resultCode){         case R.id.common_backToActivity1:             closeActivity();  //Finish Activity2 in this method             break;           }         super.onActivityResult(requestCode, resultCode, data);     } 

Then you will get directly to Activity1.



回答2:

For the first question

  1. use finish() in activity 2 after you call activity 3.

  2. you should probably close the application when you hit home button. Because your application will retain it's state until it's closed.



回答3:

Try something like this:

Home Key 

You can not control the behaviour of Home key. When Home key is pressed your app is sent to background and when you launch your app again then it comes to the state from where you have left. But this behaviour is not consistent as it is device dependent. Device can finish() your app if it needs memory or any resource which your app is using.

for your activity: 

start activity 3 with startActivityForResult() and on back press finish() activity 3 and when you get the call in onActivityResult() in activity 2 finish() it there and you will be back to activity 1.



回答4:

For your first question. You could override your back button and check which activity you are currently in and handle where to go based on that.

To your second question.

Put android:clearTaskOnLaunch="true" in your startup activity in the manifest file. That will make your application always start over when you relaunch it.



回答5:

You should implement your Intent calls to follow this. Rather than moving from Activity 2 to Activity 3, you should have Activity 2 finish and return some value that prompts Activity 1 to then call Activity 3. As for when you press home, add the same code to your OnResume() OnCreate() areas so that it knows which intent/Activity to always load.



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