Search code examples
androidxmlandroid-studioandroid-activityandroid-manifest

How do you choose the starting file in Android?


***I looked at another post about this but it didn't work for me.

I have two files in Android Studio and I want to make StartActivity show up when the user opens the app. However, MainActivity is coming up. I don't see the problem with my code. My AndroidManifest.xml is below. Thanks in advance! Please let me know if any more information is needed.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simplysnap">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".LoginActivity" />
        <activity android:name=".StartActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

My startactivity file:

package com.example.simplysnap;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class StartActivity extends AppCompatActivity {

    Button login, register;

    FirebaseUser firebaseUser;
    @Override
    protected void onStart() {
        super.onStart();

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        //if user has no value then redirect
        if (firebaseUser != null){
            startActivity(new Intent(StartActivity.this, MainActivity.class));
            finish();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        login = findViewById(R.id.login);
        register = findViewById(R.id.register);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(StartActivity.this, LoginActivity.class));
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(StartActivity.this, RegisterActivity.class));
            }
        });
    }
}

My mainactivity file:

package com.example.simplysnap;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Solution

  • first go to the (Build->Clean Project) it will clean the project and delete all the temp. files, Gradle file and generated files and (Build->Rebuild Project) rebuild the project it will regenerate all file needed to run the app then try to run

    this will remove the previous version of the app and will regenerate the output file

    //if user has no value then redirect
            if (firebaseUser != null){
                startActivity(new Intent(StartActivity.this, MainActivity.class));
                finish();
            }
    

    this is where it is redirecting may be if you have added a firebase user previous run and you probably uninstall the app and trying to install the app again it will take previous user so you need uninstall the app and restart the device it will clean the firebase user