我有四個活動 A-> B-> C-> d 其中A是B的父,C B和C D. 的我有指定的onCreate動畫和onBackPressed活動對custoized返回堆棧重建
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);}
和
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_left,R.anim.push_out_right);
}
在從一些其它活動用戶輸入我使用發射活動d以下代碼
Intent intent=new Intent(this,D.class);
TaskStackBuilder.create(this).addNextIntentWithParentStack(intent).startActivities();
如預期的那樣D啓動並且C,B,A被添加到backstack.But當我從D動畫中按下時,onCreate中指定的動畫被觸發而不是onBackPressed.Also中的動畫,當我從B按下回到A onBackPressed動畫被觸發。這不會發生,如果我從A導航到D,然後我按回,所以onCreate中的動畫接管onBackPressed中的動畫。所以發生了什麼以及如何解決這個問題? 預先感謝您
我使用A,B這個抽象的活動,C
public abstract class AbstractActivity extends ActionBarActivity implements View.OnClickListener{
int listId;
String[] data=new String[]{};
RecyclerView.Adapter adapter;
List<String> list=new ArrayList<>();
String title;
String resourceName;
int holderId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
setContentView(R.layout.recycler);
Toolbar toolbar=(Toolbar)this.findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle(title);
setSupportActionBar(toolbar);
if (listId==0)
listId=this.getResources().getIdentifier(resourceName,"array",this.getPackageName());
data=this.getResources().getStringArray(listId);
for(int i=0;i<data.length;i++)
list.add(data[i]);
RecyclerView recyclerView=(RecyclerView)this.findViewById(R.id.recycler_view);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this,null));
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter=new CardAdapter(list,holderId,this);
recyclerView.setAdapter(adapter);
}
public AbstractActivity(String title,int listId,int holderId){
/*
title is the title for the activity
listId is the string array resource id for data used by the recyclerView
holderId is the layout resource used as viewHolder by the recycler used by the CardAdapter
*/
this.title=title;
this.listId=listId;
this.holderId=holderId;
}
我extendig該類活性A(MainActivity):
public class MainActivity extends AbstractActivity {
Class activityClass;
public MainActivity(){
super("VTU Student",R.array.main_page_options,R.layout.main_page);
}
@Override
public void onClick(View v) {
String item= (String) v.getTag();
final String className=item.replaceAll(" |\\.","");
try {
activityClass=Class.forName("android.anoop.com.vtustudent."+className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
final Context context=this;
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(context,activityClass);
startActivity(intent);
}
},600);
}
}
活動B和C還擴展了AbstractActivity。
至於活動d(主體):
public class Subjects extends ActionBarActivity implements View.OnClickListener{
String branch="";
int listId;
String[] data=new String[]{};
List<String> list=new ArrayList<>();
RecyclerView.Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
branch=getIntent().getStringExtra("branch");
setContentView(R.layout.recycler);
Toolbar toolbar=(Toolbar)this.findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
toolbar.setTitle("Subjects");
setSupportActionBar(toolbar);
listId=getResources().getIdentifier(branch,"array",getPackageName());
data=this.getResources().getStringArray(listId);
for(int i=0;i<data.length;i++)
list.add(data[i]);
RecyclerView recyclerView=(RecyclerView)this.findViewById(R.id.recycler_view);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, null));
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter=new CardAdapter(list,R.layout.recycler_list_holder,this);
recyclerView.setAdapter(adapter);
}
@Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
}
和動畫文件是: pull_in_left.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duration="700"
android:fromXDelta="-100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
pull_in_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duration="700"
android:fromXDelta="100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
push_out_left:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duration="700"
android:fromXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="-100%" />
push_out_right:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duration="700"
android:fromXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="100%" />
onCreate被激發,因爲backstack中的活動從未創建過。每次活動都是在按下後退時創建的。您必須在styles.xml文件 – Neil 2015-03-02 11:58:42
中更改活動主題中的活動輸入/退出動畫,那麼startActivities()會做什麼?此外,對於B-> A而不是D-> C或C-> B觸發onBackPressed動畫,這是我無法理解的 – AnoopDV 2015-03-02 12:01:14