Java “继续”按钮不工作,无法转到上一个活动

Java “继续”按钮不工作,无法转到上一个活动,java,android,android-studio,android-intent,android-activity,Java,Android,Android Studio,Android Intent,Android Activity,有4个活动:主活动、p1、p2、p3 我的简历按钮不工作。我想在我点击“恢复”按钮时打开我的上一个活动。 例如:在p2中单击转到主屏幕,然后在主屏幕中单击恢复,p2打开。这是我的代码: 主要活动: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan

有4个活动:主活动、p1、p2、p3 我的简历按钮不工作。我想在我点击“恢复”按钮时打开我的上一个活动。 例如:在p2中单击转到主屏幕,然后在主屏幕中单击恢复,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);
            }
        });
    }

}
<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>
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();
    }

}
<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>
XML布局:

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);
            }
        });
    }

}
<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>
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();
    }

}
<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>
XML:

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);
            }
        });
    }

}
<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>
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();
    }

}
<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>

你必须做以下事情才能实现你想要的

  • 无论何时打开“活动”,都必须在前言中保存以前的活动名称
  • 当您按下“恢复”按钮时,从prefrance获取活动名称,并使用以下代码打开“活动”

    意图=新意图(); intent.setClassName(“com.android.browser”、“com.android.BrowserActivity”); 背景。开始触觉(意图); //开始前清除以前的活动

  • 当您重新启动应用程序时,请在主活动中再次执行步骤2


  • 在这种方法中,您必须在启动活动之前使用单音或清除堆栈来维护应用程序流。因为您必须处理backbutton

    您是否在
    AndroidManifest.xml
    中注册了所有活动?我将其放在resume按钮中:Intent Intent=new Intent();intent.setClassName(“com.android.browser”、“com.android.BrowserActivity”);背景。开始触觉(意图);