是Intent.FLAG_ACTIVITY_REORDER_TO_FRONT的替代品

时间:2014-08-14 11:02:35

标签: android android-intent android-activity start-activity

我正在创建以下两个活动,当点击MainActivity中的按钮时,它显示SecondActivity,而来自SecondActivity的点击按钮正常显示MainActivity但现在点击软后退按钮Device show ANR。

MainActivity按钮单击 - > SecondActivity按钮单击 - >软后退,结果ANR。

有解决方法吗?

Android 4.4.4的这个问题。我知道这件事 https://code.google.com/p/android/issues/detail?id=63570#c2 问题。

感谢您的帮助。

  public class MainActivity extends Activity {
        TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.textView1);
            tv.setText("Main Activity");
        }

        public void onClickBtn(View view)
        {
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }

        public void finish(){
            Log.v("MainActivity", "Finish of main activity called");
            super.finish();
        }
    }


    public class SecondActivity extends Activity {
        TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.textView1);
            tv.setText("Second Activity");
        }

        public void onClickBtn(View view) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
        }

        public void finish(){
            Log.v("SecondActivity", "Finish of second activity called");
            super.finish();
        }
    }

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="42dp"
        android:onClick="onClickBtn"
        android:text="Button" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:text="@string/hello_world" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

使用intent.setFlags (Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);应用程序不会崩溃。