0

Let's say we have Screen A. When the application first loads, the user is taken to Screen A. Screen A has a list of items that lead to Screen B, C, D, or E. When user presses one of these items, they are guided to whichever screen they chose. Then when the user closes the app or even turns off their phone and reloads the app later, they are taken to the screen which they picked in the first place instead of Screen A. I was wondering how I would be able to do this. I'll provide a more practical example with code:

//Default Launcher
public class ScreenA extends Activity {
    ListView listView ;

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

        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.listView);

        // Defined Array values to show in ListView
        String[] values = new String[] { "B","C","D", "E"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter);

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                int itemPosition     = position;
                String  itemValue    = (String) listView.getItemAtPosition(position);

                if (itemValue.equals("B")){
                    finish();
                    Intent nextScreen = new Intent(getApplicationContext(), B.class);
                    startActivity(nextScreen);
                } else if (itemValue.equals("C")){
                    finish();
                    Intent nextScreen = new Intent(getApplicationContext(), B.class);
                    startActivity(nextScreen);
                } else if (itemValue.equals("D")){
                    finish();
                    Intent nextScreen = new Intent(getApplicationContext(), D.class);
                    startActivity(nextScreen);
                } else if (itemValue.equals("E")){
                    finish();
                    Intent nextScreen = new Intent(getApplicationContext(), E.class);
                    startActivity(nextScreen);
                }

            }

        });
    }

}

After user selects either of these options, they are guided to the screen that they chose. When they leave the application and come back to it later, the screen that they chose loads up instead of the screen with the list of items. If they want to choose another screen later, they can access the settings and choose whichever screen they like (I already have a button to the listview in one of the action bar overflow options).

Scenario: If user selects "B", they are taken to Screen "B". If they close the application, turn off the phone, and come back to the application later, Screen "B" will load instead of Screen "A".

PS: I removed a lot of code and replaced names from the actual code to try to make it simpler. If there's a problem in the code, please let me know.

dunce1
  • 323
  • 5
  • 21

2 Answers2

0

Your default activity is set in the manifest, and that can't be changed. What you can do is change this default activity to be some sort of launcher, that will open activity A, B or C.

To know which activity to open, you can save the last opened activity in the SharedPreferences.

If you need to save the state of the activity, you can use onSaveInstanceState()

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0

If I were you, I'd store activity name as a String in onPause using SharedPreferences :

Editor edit = prefs.edit();
edit.putString("act", "activityToStart");
edit.commit();

And in your onCreate you can do :

String activityToStart = "";

if(prefs.hasString("act")) {

    activityToStart = prefs.getString("act");

}

if(activityToStart.equals("B") {
    startActivity(new Intent(A.this, B.class));

}

I didn't test the code but I think it should work. Hope it helps :)

Muhammad Ashraf
  • 1,252
  • 1
  • 14
  • 32