S Kharbanda S Kharbanda - 3 months ago 8
Java Question

Start Activity every time app is launched

My app opens

MainActivity
but I actually want to open
LoginActivity
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) {
e.printStackTrace();
}

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


And

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)
.commit();
}

Answer

Set your Activity in your Manifest file such as:

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

        <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
</activity>