我的應用程序工作正常,但我的目的是,當我關閉應用程序,然後再次運行它,我的應用程序在最後一個活動打開。我想在我再次打開主要活動時顯示,如果我點擊了簡歷然後上次活動打開。所以,我有4 Activity
稱爲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,P1那樣
例如P3:當我在P2然後點擊轉到主,主當點擊退出並重新運行我的應用程序,我的應用程序打開的P2我想當我重新運行應用程序的主要活動,如果我點擊簡歷,然後p2來顯示。
ü可以從我的代碼解釋呢?所以我把私人無效存儲當前活動在我的主要按鈕去?和另一個代碼...只是告訴我,我必須在我的代碼中使用此代碼,請致謝 – erfan
您應該在您退出應用程序時始終調用「storeCurrentActivity()」。 –
這段代碼對我來說什麼都沒有 – erfan