如何使用恢复按钮进行上一个活动? [英] How can go in last activity with resume button?
问题描述
我的应用程序运行正常,但是我的目的是当我关闭该应用程序然后再次运行时,我的应用程序在上一次活动时打开.我想在再次打开时显示主要活动,如果我单击简历",则最后一个活动打开.所以我有4个Activity
称为Main Activity (my main)
,p1
,p2
和p3
:
My app works fine, but my purpose is that when I close the app then run it again, my app opens at the last activity. I want when I open again main activity become to show and if I clicked resume then last activity open. So I have 4 Activity
called Main Activity (my main)
, p1
,p2
, p3
:
主要活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
Button resume=(Button)findViewById(R.id.resume);
Button next=(Button)findViewById(R.id.next);
Button exit=(Button)findViewById(R.id.exit);
resume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, p1.class);
startActivity(intent);
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="resume"
android:layout_width="wrap_content"
android:id="@+id/resume"
android:layout_height="wrap_content" />
<Button
android:text="next"
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"/>
</LinearLayout>
p1:
public class p1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p1);
Button next = (Button) findViewById(R.id.next);
Button home=(Button)findViewById(R.id.home);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(p1.this,p2.class);
startActivity(intent);
}
});
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(p1.this,MainActivity.class);
startActivity(intent);
}
});
}
}
p1 XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="page 1"/>
<Button
android:text="go in main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home"/>
</LinearLayout>
和p2,p3就像p1
and p2, p3 like p1
例如:当我在p2中,然后单击进入main并在main中单击退出并重新运行我的应用程序时,我的应用程序在p2中打开我想要在我重新运行应用程序的主要活动时,如果我单击简历然后p2打开来展示.
for example : when I am in p2 then click in go to main and in main when click exit and re run my app , my app open in p2 i want when i re run app main activity open if i clicked resume then p2 come to show.
推荐答案
要在重新启动时打开主要活动,请致电
To open main activity when relaunch you should call
private void FinishActivity(){
this.finish();
}
在startActivity(intent);
用于恢复您的活动,将当前活动类存储为共享首选项的字符串
for resume your activity store current activity class as String to shared preference
在moveTaskToBack(true);
private void storeCurrentActivity(){
SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPref.edit();
editor.clear();
String packageName = this.getPackageName();
String className = this.getClass().getSimpleName();
editor.putString("lastactivity",packageName+"."+className);
editor.commit();
}
并将结果存储到共享的首选项
and store result in to a shared preference
当您返回到应用程序时,只需检查SharedPreference
when you comeback to your application just check the SharedPreference
在setContentView(R.layout.main_page);
String lastActivity= getLastActivity();
try {
Intent fooActivity = new Intent(this,Class.forName(lastActivity))
startActivity(fooActivity)
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
将此方法放入MainActivity
place this method in your MainActivity
private String getLastActivity(){
SharedPreferences myPref = = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
String lastActivity= myPref.getString("lastactivity","");
}
这可能对您有帮助
此特定问题的解决方案
用以下代码替换您的主页按钮操作
Replace your home button action with below code
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(p1.this,MainActivity.class);
startActivity(intent);
storeCurrentActivity();
this.finish();
}
});
上面的代码将存储您的活动,即,如果您处于活动p1中,它将存储p1作为上一个活动
the above code will store your actiivty,ie if you are in activity p1,it will store p1 as last activity
还请删除onResume
操作
这篇关于如何使用恢复按钮进行上一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!