问题
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
:
Main Activity:
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 layout :
<?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 layout:
<?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>
and p2, p3 like p1
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.
回答1:
To open main activity when relaunch you should call
private void FinishActivity(){
this.finish();
}
in your onClick events after startActivity(intent);
for resume your activity store current activity class as String to shared preference
Call below method in your code before 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
when you comeback to your application just check the SharedPreference
use below code in your main activity after setContentView(R.layout.main_page);
String lastActivity= getLastActivity();
try {
Intent fooActivity = new Intent(this,Class.forName(lastActivity))
startActivity(fooActivity)
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
place this method in your MainActivity
private String getLastActivity(){
SharedPreferences myPref = = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
String lastActivity= myPref.getString("lastactivity","");
}
this may help you
Solution For this specific problem
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();
}
});
the above code will store your actiivty,ie if you are in activity p1,it will store p1 as last activity
also please remove onResume
Action
回答2:
I will use the Clifford's response to propose a solution for you, since you could not understand his answer, although it was very clear to me.
The method above will store the activity on SharedPreferences
private void storeCurrentActivity(){
SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = myPref.edit();
editor.putString("lastactivity",this.getClass().getSimpleName());
editor.commit();
}
Put this method and call him in all onResume in every Activity, except on Main:
@Override
public void onResume(){
super.onResume();
storeCurrentActivity();
}
In your resume button, just call the last saved Activity
resume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences myPref = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
String lastActivity= myPref.getString("lastactivity","");
try {
Intent fooActivity = new Intent(this,Class.forName(lastActivity))
startActivity(fooActivity)
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
回答3:
so after some try this was my answer of my question
Main activity :
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) {
String lastActivity= getLastActivity();
try {
if(!lastActivity.equals(""))
{
Toast.makeText(getApplicationContext(),"previous activity :"+lastActivity, Toast.LENGTH_SHORT).show();
Intent fooActivity = new Intent(MainActivity.this,Class.forName(lastActivity));
startActivity(fooActivity);
MainActivity.this.finish();
} else {
Toast.makeText(getApplicationContext(),"no previous activity !", Toast.LENGTH_SHORT).show();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
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) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
private String getLastActivity(){
SharedPreferences myPref = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
String lastActivity= myPref.getString("lastactivity","");
return lastActivity;
}
}
p1,p2,p3 :
public class p1 extends Activity {
@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);
storeCurrentActivity();
startActivity(intent);
p1.this.finish();
}
});
}
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();
}
}
来源:https://stackoverflow.com/questions/38609179/how-can-go-in-last-activity-with-resume-button