我有四项活动 A-> B-> C-> d 其中A是B的父,B是C,C是D. 我有为onCreate和onBackPressed指定的动画
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。但是当我从onCreate中指定的D动画按回来而不是onBackPressed中的动画时。也就是当我从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;
}
我在活动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%" />
答案 0 :(得分:0)
它有点像黑客,但我无法找到实际发生的事情。 我在按下后面时设置了一个sharedPreference:
@Override
public void onBackPressed(){
super.onBackPressed();
SharedPreferences.Editor editor=getSharedPreferences("OtherInfo",Context.MODE_PRIVATE).edit();
editor.putBoolean("backPressed",true);
editor.commit();
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
}
然后在其父活动中,我检查了backPressed,如果它是假(当没有按下后退时)我执行动画代码,否则我将backPressed设置为false,这样当从其父项输入活动时它可能不会干扰活性:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!getSharedPreferences("OtherInfo",Context.MODE_PRIVATE).getBoolean("backPressed",false)) {
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
}
else
{
SharedPreferences.Editor editor=getSharedPreferences("OtherInfo",Context.MODE_PRIVATE).edit();
editor.putBoolean("backPressed",false);
editor.commit();
}
这是按预期给出的结果。