خرید بک لینک

Vote count: 0

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

asked 47 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 9 مرداد 1395 ساعت: 15:28

صفحه بندی