S Kharbanda S Kharbanda -4 years ago 155
Java Question

Start Activity every time app is launched

My app opens

but I actually want to open
when the app is launched. I've tried some solutions like:

PackageInfo info = null;
try {
info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
} catch (PackageManager.NameNotFoundException e) {

int currentVersion = info.versionCode;
this.versionName = info.versionName;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int lastVersion = prefs.getInt("version_code", 0);
if (currentVersion > lastVersion) {
prefs.edit().putInt("version_code", currentVersion).apply();
startActivity(new Intent(ScheduleActivity.this, LoginActivity.class));


boolean firstboot = getSharedPreferences("BOOT_PREF",MODE_PRIVATE)
.getBoolean("firstboot", true);

if(firstboot) {
//place your code that will run single time
startActivity(new Intent(ScheduleActivity.this, LoginActivity.class));
getSharedPreferences("BOOT_PREF", MODE_PRIVATE).edit().
putBoolean("firstboot", false)

Answer Source

Set your Activity in your Manifest file such as:

<activity android:name=".LoginActivity">
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download