2

have 4 activity : Main Activity,p1,p2,p3 my resume button not working . i want when i click in resume button app open my last activity . for example : when in p2 click go to main , then in main when click resume , p2 open . here is my code :
Main Activity :

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 layout :

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

and p2,p3 like p1 and this my manifast :

<?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>
Erfan
  • 3,059
  • 3
  • 22
  • 49

1 Answers1

0

you have to do below things to achieve what you want

  1. whenever you open activity you have to save previous activity name in prefrance.
  2. when you press resume button get activity name from prefrance and open activity using below code

    Intent intent = new Intent(); intent.setClassName("com.android.browser","com.android.BrowserActivity"); context.startActivity(intent); // clear previous activity before start

  3. when you app relaunch app then do step 2 again in main Activity

in this type of approch you have to maintain flow of application using singletone or clear stack before launch activity. because you have to handle backbutton

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • i put this in resume button : Intent intent = new Intent(); intent.setClassName("com.android.browser","com.android.BrowserActivity"); context.startActivity(intent); – Erfan Jul 31 '16 at 07:39