我想在任务历史记录堆栈中有这种行为:
这样,如果用户按下后退按钮,他会返回B,然后再次按下后退按钮返回A(始终是A的相同实例)。
使用意图标志“FLAG_ACTIVITY_REORDER_TO_FRONT”我已经实现了这一行为:
所以A的实例实际上是在前面,但是在带有后退按钮的B之后,A不再在B和Main之间,因此显示Main。
谢谢!
答案 0 :(得分:2)
我认为您理想的情况通常是不可能的。因为Tasks and Back Stack说:
当用户按下“返回”按钮时,将弹出当前活动 从堆栈的顶部(活动被销毁)
因此,如果您的任务是A - > B - > A(重复)和用户按下后退按钮,他回到B,A 被破坏,他不能回到A.
我写了具有这种行为的树活动A,A1,A2:
主要 - > A - > A1 - > A2 - > A1(重复)
接下来当用户在A2中按BACK时:
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,所以我在A1中添加额外的意图并覆盖onNewIntent(Intent intent)我的项目清单是:
<?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>
注意&#34; singleInstance &#34;和&#34; singleTask &#34;活动元素中的属性。最后,您可以将此布局用于您的活动,以确保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>
答案 1 :(得分:0)
您可以在开始新活动后立即尝试使用finish();
:
startActivity(intent);
finish();