簡體   English   中英

在后台重建的活動

[英]Activities on the custoized backstack recreating

我有四個活動A-> B-> C-> D其中A是B的父,C是B,C是C.我有為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%" />

它有點像黑客,但我無法找到實際發生的事情。 我按下后面時設置了一個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();
    }

這是按預期給出的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM