恢复按钮无法进行上一次活动

时间:2016-07-30 08:13:38

标签: java android android-studio android-intent android-activity

有4项活动:主要活动,p1,p2,p3 我的简历按钮不起作用。我想当我点击恢复按钮应用程序打开我的上一个活动。 例如:在p2中单击转到main,然后在main中单击resume,p2打开。这是我的代码:
主要活动:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);

        Button resume = (Button) findViewById(R.id.resume);
        Button next = (Button) findViewById(R.id.next);
        Button exit = (Button) findViewById(R.id.exit);

        resume.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            SharedPreferences myPref = getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
            String lastActivity= myPref.getString("lastactivity","");
            try {
                Intent fooActivity = new Intent(MainActivity.this,Class.forName(lastActivity));
                startActivity(fooActivity);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }


        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, p1.class);
                startActivity(intent);
            }
        });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveTaskToBack(true);
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            }
        });
    }

}

XML布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="resume"
        android:layout_width="wrap_content"
        android:id="@+id/resume"
        android:layout_height="wrap_content" />

    <Button
        android:text="next"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="exit"/>
</LinearLayout>

p1:

public class p1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1);

        Button next = (Button) findViewById(R.id.next);
        Button home=(Button)findViewById(R.id.home);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(p1.this, p2.class);
                startActivity(intent);

            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(p1.this, MainActivity.class);
                startActivity(intent);

            }
        });

    }
    private void storeCurrentActivity(){
        SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = myPref.edit();
        editor.putString("lastactivity", this.getClass().getSimpleName());
        editor.commit();
    }
    @Override
    public void onResume(){
        super.onResume();
        storeCurrentActivity();
    }

}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/next"/>
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="page 1"/>
    <Button
        android:text="go in main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/home"/>

</LinearLayout>

和p2,p3与p1相同 这是我的表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.er.myapplication">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity

            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".p1"
            android:label="@string/title_activity_main2"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".p2"
            android:label="@string/title_activity_p2"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".p3"
            android:label="@string/title_activity_p3"
            android:theme="@style/AppTheme.NoActionBar"/>
    </application>
</manifest>

1 个答案:

答案 0 :(得分:0)

你必须做下面的事情来实现你想要的东西

  
      
  1. 每当您打开活动时,您必须在prefrance中保存以前的活动名称。
  2.   
  3. 按下继续按钮从prefrance获取活动名称并使用以下代码打开活动

         

    Intent intent = new Intent();   intent.setClassName(&#34; com.android.browser&#34;&#34; com.android.BrowserActivity&#34);   context.startActivity(意向);   //在开始之前清除之前的活动

  4.   
  5. 当您重新启动应用时,请在主要活动

  6. 中再次执行第2步   

在这种类型的approch中,您必须在启动活动之前使用单线程或清除堆栈来维护应用程序的流程。因为你必须处理后退按钮

相关问题