StevenN
StevenN

Reputation: 11

Having more than one Launcher Activity

I'm working on an App which can read out car data.
When the user opens it the first time he must choose the car he drives (this is in MainActivity).
What I want to do is, that the user must not always choose his car when opening the App.
The App should directly go to the car data Activity of his car after the user chose the car once.
Can you please give me some ideas how to do that?

I already wrote in the AndroidManifest that MainActivity and this Car Data Activity are Launcher Activities but I think it will not work because how should the App know which Activity should be launch Activity.
Please help me a bit!

Upvotes: 0

Views: 265

Answers (3)

Anton Molganov
Anton Molganov

Reputation: 1

You should create two activities:
- MainActivity (marked as launcher activity in manifest) where you show car data.
- CarChooseActivity where you choose car.

In MainAcitivity on onResume() method try to read car data (from SharedPreferences or other source). If this succeeded then show you car data, otherwise open CarChooseActivity.

Something like that:

public class MainActivity extends AppCompatActivity {

//code omitted

    @Override
    protected void onResume() {
        super.onResume();


        Car car = readCar();
        if (car == null){//no car saved
            Intent i = new Intent(this, CarChooseActivity.class);
            startActivity(i);
        }

    }

//code omitted
}


public class CarChooseActivity extends AppCompatActivity {

    Button mSaveButton;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        //code omitted

        mSaveButton = findViewById(R.id.savebutton);
        mSaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveCar();//save choosed car to SharedPreferences or other storage
                finish();
            }
        });

        //code omitted
    }
}

Upvotes: 0

Hitesh Raviya
Hitesh Raviya

Reputation: 129

You can use SharedPreference for this process.

SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(YourLaunchActivity.this);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("isCarSet", true);
editor.apply();

Then Check Everytime Launch activity

if (sharedpreferences.getBoolean("isCarSet", false)) {
    Intent i =new Intent(YourLaunchActivity.this,SecondActivty.class);
    startActivity(i);
    finish();
    }

I will suggest you use Shared Preference.

Upvotes: 1

Jatin Bhatia
Jatin Bhatia

Reputation: 258

So what you can do in this case is that you can use Shared Preferences. Once the data is registered in Shared Preference, every time the app is started read the data from Shared Preference and go directly to the desired page.

Sample code for Shared Preference:

Setting values in Preference:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.apply();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

more help at: https://developer.android.com/guide/topics/data/data-storage#pref

The other way around is to use sessions, but being at a nascent stage, I would suggest you to use Shared Preference.

Upvotes: 0

Related Questions