-4

My App flow is:

Start app with SplashActivity -> LoginActivity -> MainActivity

I use session manager class to check remember button and now its flow is:

SplashActivity -> MainActivity

But I need to Login if check is true, then Next time when I open my app it will directly start from the MainActivity.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
AsifAli
  • 1
  • 4

2 Answers2

0

Basically you can not change the defined in intent-filter on run time.. but there is a way to achieve what you need using <activity-alias>

As described in this answer, you need to follow these steps:

  1. Make your SplashActivity have the LAUNCHER <intent-filter> and declare your MainActivity with no <intent-filter>.
  2. make an <activity-alias> element in the manifest pointing to MainActivity that has the LAUNCHER <intent-filter>.

  3. Put android:enabled="false" on the <activity-alias>, so it is disabled by default, so when the app is first installed or the user is not logged in, the SplashActivity.

  4. When the user logs in, and you want to change so MainActivity is the launcher activity, use PackageManager and setComponentEnabledSetting() to make the <activity-alias> enabled and to disable the SplashActivity.

    // after login/logout success enable/disable components as required
    // use the correct "packagename", "alias" and activities names
    getPackageManager().setComponentEnabledSetting(
                                new ComponentName("packagename", "alias"),
                                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
                                PackageManager.DONT_KILL_APP);
    try {
        getPackageManager().setComponentEnabledSetting(
                                new ComponentName("packagename", "SplashActivity"),
                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                PackageManager.DONT_KILL_APP);
    
    } catch (Exception e) {
         e.printStackTrace();
    }
    

    define your alias like this

    <activity-alias
        android:name=".Blahblah"
        android:targetActivity=".MainActivity"
        android:label="@string/splashactivity_name"
        android:icon="@drawable/icon_splashactivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity-alias>
    

    with this when you enable your alias it'll work as your launcher and it'll make the application start with MainActivity

When the user logs out you can do the same thing to change back the default configuration

ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • "Application not found" is giving when click on app – AsifAli May 15 '18 at 15:19
  • How did you define your activity-alias? did use provide the right target activity and intent-filter? – ColdFire May 15 '18 at 15:21
  • In Manifest – AsifAli May 15 '18 at 15:35
  • The intent-filter launcher should be set for the alias and not the splash activity.. you need to uninstall/reinstall app to reset the change for test – ColdFire May 15 '18 at 15:48
  • Sorry to Disturb you again @A.A but i tell you from initial steps My Scenario is app Start from (step 1) Splash Activity than Login Activity is Open,(step 2) On Login Activity I have remember me checkbox. If user checked = true than press loginBtn than Session save this value and when again app Start than it should open MainActivity other wise app open as it from step(1). – AsifAli May 15 '18 at 17:05
  • I know.. but you need to have your activity-alias have a launcher intent filter too so when you enable you can start with MainActivity without getting "application not found".. also choose another name for your activity-alias – ColdFire May 16 '18 at 08:05
  • Thanks #A.A but i found an other way to do this I'm using handler class On Splash in final block i set to move in login and further in Main Class Now i set checked on splashClass which check session have data or not if preset than redirect to Main Class other wise process continue as it and from logout buton in mainClass i clear session data – AsifAli May 16 '18 at 11:08
0

I found an other way to do this I'm using handler class On Splash in final block i call LoginClass and further move in MainClass Now I changed some code. First It checked on SplashClass that SessionClass have data or not. If data present than redirect to MainClass and from Logout button in MainClass I clear session. If data not present, process start from beginning .

AsifAli
  • 1
  • 4