我認爲你需要的情況下,通常是Tasks and Back Stack impossible.Because說:
當用戶按下後退按鈕,當前的活動是從堆棧的頂部彈出 (活性被破壞)
所以,如果你的任務是A - >乙 - >(一式兩份)和用戶按下後退按鈕時,他又回到B和A的破壞,他不能回去給A.
我寫樹活動A,A1,A2具有此行爲:
首頁 - > A - > A1 - > A2 - > A1(複製)
接下來,當用戶返回在A2按:
A1 - > A2 - > A1(相同) - > A - >主
這是他們的代碼:
活動A爲:
package t.t;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TaskTestActivity extends Activity {
Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main0);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(TaskTestActivity.this, TaskTestActivity1.class);
i.putExtra("loader", "A");
TaskTestActivity.this.startActivity(i);
}
});
}
}
活動A1是:
package t.t;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
public class TaskTestActivity1 extends Activity {
Button btn;
String str = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(TaskTestActivity1.this, TaskTestActivity2.class);
i.putExtra("loader", "A1");
TaskTestActivity1.this.startActivity(i);
}
});
}
@Override
public void onBackPressed() {
System.out.println(str);
if(str.equals("ali")){
Intent i = new Intent(this,TaskTestActivity2.class);
this.startActivity(i);
str = "";
System.out.println("BACK");
}else{
Intent i = new Intent(this,TaskTestActivity.class);
this.startActivity(i);
str = "";
System.out.println("BACK1");
}
}
@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
str = "ali";
}
}
活動A2是:
package t.t;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
public class TaskTestActivity2 extends Activity {
Button btn;
int i = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(TaskTestActivity2.this,
TaskTestActivity1.class);
i.putExtra("loader", "A2");
TaskTestActivity2.this.startActivity(i);
}
});
}
}
請注意,我在活動A1覆蓋onBackPressed(),你會檢測要回A2或A,所以我額外加給的意圖並覆蓋onNewIntent(意向意圖)在A1.My項目清單是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="t.t"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:hardwareAccelerated="true">
<activity
android:label="A"
android:launchMode="standard"
android:name=".TaskTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="A2"
android:launchMode="singleInstance"
android:name=".TaskTestActivity2" >
</activity>
<activity android:name="TaskTestActivity1" android:launchMode="singleTask" android:label="A1"></activity>
</application>
</manifest>
關注「singleInstance」和「singleTask」活動elements.Finally屬性,你可以使用此佈局,爲您的活動,以確保A1是複製:
的main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
main1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
main0.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>