-1

As the title stated above, I've had no idea what else to do. When I click the run button the app starts in a wrong activity, but when I clear the app data and cache and start manually on my phone it shows the intended activity I wanted. This is my manifest

<application
    android:allowBackup="true"
    android:icon="@drawable/icon_logo"
    android:label="@string/app_name"
    android:roundIcon="@drawable/icon_logo"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashScreen"
        android:theme="@style/MyThemeNoBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />
    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login" />

The app will directly go to MainActivity where it's based on Tabbed Activity from the template of Android Studio.

EDIT

Here is my SplashScreen code

public class SplashScreen extends AppCompatActivity {

    SharedPreferences settings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Create SharedPreferences to store token
        settings = getSharedPreferences("token", Context.MODE_PRIVATE);
        //Check if user session is stored or not
        if (settings.getBoolean("connected", true)) {
        /* The user has already login, so start the dashboard */
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        Thread myThread = new Thread(){
            @Override
            public void run() {
                try {
                    sleep(3000);
                    Intent intent = new Intent(SplashScreen.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        myThread.start();
    }
}

EDIT

OK, the flow of my app should be like this SplashScreen -> MainActivity -> LoginActivity. Currently the app skip past SplashScreen and goes directly to MainActivity. Below are my build.gradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.public.test"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:26.+'
    compile 'com.auth0.android:jwtdecode:1.1.1'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.google.android.gms:play-services-maps:11.0.2'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.android.support:support-vector-drawable:26.+'
    testCompile 'junit:junit:4.12'
}
Gooner
  • 369
  • 5
  • 16

2 Answers2

0

try by doing something like this

in your manifest edit your splashScreen Activity Intent filter

<activity
        android:name=".SplashScreen"
        android:theme="@style/MyThemeNoBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

and in your MainActivity tag try using

<activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="your complete.name.package.MAINACTIVITY" />

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

ya change the name to wherever you have kept your main activity i.e full specified path

MrCurious
  • 150
  • 3
  • 11
0

After intensive code analyzing, I've finally found what causes the problem. This line of code had been the problem that cause my page to start of a wrong activity.

if (settings.getBoolean("connected", true)) {
    /* The user has already login, so start the dashboard */
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

And the reason why it happend is because the if block detect the connect as true even when there is no SharedPref file was created in fresh deployment. The solution I've used is to change the if statement of setting.getBoolean to setting.contain

if (settings.contain("connected")) {
    /* The user has already login, so start the dashboard */
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

There is a strange feeling lingering in my mind as to why the getBoolean()return true even the "connected" had not been created yet, plus I also disabled the android:allowBackup:"false"that should had solve the problem. By the way thanks for all the response.

Gooner
  • 369
  • 5
  • 16