erfan erfan - 4 months ago 17
Java Question

How can go in last activity with resume button?

My app works fine, but my purpose is that when I close the app then run it again, my app opens at the last activity. I want when I open again main activity become to show and if I clicked resume then last activity open. So I have 4

Activity
called
Main Activity (my main)
,
p1
,
p2
,
p3
:

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


if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {

finish();
return;
}


}
});

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 :

<?xml version="1.0" encoding="utf-8"?>
<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 AppCompatActivity {

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

}
});

}
}


p1 XML layout:

<?xml version="1.0" encoding="utf-8"?>
<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

for example : when I am in p2 then click in go to main and in main when click exit and re run my app , my app open in p2 i want when i re run app main activity open if i clicked resume then p2 come to show.

Answer

so after some try this was my answer of my question
Main activity :

public class MainActivity extends Activity {

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


    final 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) {


            final String PREFS_NAME = "MyPrefsFile";

            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

            if (settings.getBoolean("my_first_time", true)) {

                resume.setEnabled(false);

                Log.d("Comments", "First time");


                settings.edit().putBoolean("my_first_time", false).commit();
            }else
            {
                MainActivity.this.finish();
            }


        }
    });

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

            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

}


}

p1,p2,p3 :

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", p1.this.getClass().getSimpleName());
        editor.commit();
    }
    @Override
    public void onResume(){
        super.onResume();
        storeCurrentActivity();
    }

}

hope this useful ;)

Comments