简体   繁体   中英

Android Studio: 2 XML Files, but App is starting 2nd XML-file instead of 1st one

I've made an app that allows a user to login, but everytime I try to run my app, it will automatically display Activity_main.xml (that is a page that if you have login successfully).

I want to run my app, but it must display the activity_main.xml (which is the login page) first instead of the activity_main.xml which is now the first page

Here is my code:

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".FireApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">

        <activity
            android:name=".MainActivity"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".AccountActivity"
            android:theme="@style/AppTheme" />
    </application>

    </manifest>

activity_account.xml (XML for user logged in succesfully)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_account"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mertino11.ourapplication.AccountActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Account Page"
        android:ems="10"
        android:id="@+id/editText"
        android:textSize="22sp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textStyle="normal|bold"
        android:textAlignment="center" />
</RelativeLayout>

activity_main.xml (login page)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:baselineAligned="false">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/emailField"
        android:hint="Email"
        android:paddingTop="20dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/passwordField"
        android:hint="Password"
        android:fontFamily="sans-serif"
        android:paddingTop="20dp" />

    <Button
        android:text="Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/loginBtn"
        android:paddingTop="20dp" />
</LinearLayout>

Java --> Class: AccountActivity (Page when the user logged in successfully)

package com.example.mertino11.ourapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class AccountActivity extends AppCompatActivity {

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

Java --> Class: FireApp (Firebase settings I think)

package com.example.mertino11.ourapplication;

import android.app.Application;

import com.firebase.client.Firebase;

/**
 * Created by Mertino11 on 10-Dec-16.
 */

public class FireApp extends Application {

    @Override
    public void  onCreate() {
        super.onCreate();

        Firebase.setAndroidContext(this);
    }
}

Java --> Class: Main Activity (Back-end logging with account)

package com.example.mertino11.ourapplication;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class MainActivity extends AppCompatActivity {

    private EditText mEmailField;
    private EditText mPasswordField;

    private Button mLoginBtn;

    private FirebaseAuth mAuth;

    private FirebaseAuth.AuthStateListener mAuthListener;

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

        mAuth = FirebaseAuth.getInstance();

        mEmailField = (EditText) findViewById(R.id.emailField);
        mPasswordField = (EditText) findViewById(R.id.passwordField);

        mLoginBtn = (Button) findViewById(R.id.loginBtn);

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

                if(firebaseAuth.getCurrentUser() != null) {

                    startActivity(new Intent(MainActivity.this, AccountActivity.class));

                }
            }
        };

        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startSignIn();

            }
        });
    }

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

        mAuth.addAuthStateListener(mAuthListener);
    }

    private void startSignIn() {

        String email = mEmailField.getText().toString();
        String password = mPasswordField.getText().toString();

        if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {

            Toast.makeText(MainActivity.this, "Fields are empty!", Toast.LENGTH_LONG).show();

        } else {
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if(!task.isSuccessful()){

                        Toast.makeText(MainActivity.this, "Sign In Problem!", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }



    }
}

Logs in Run:

12/10 16:35:26: Launching app
$ adb push C:\Users\Mertino11\Downloads\OurApplication\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.mertino11.ourapplication
$ adb shell pm install -r "/data/local/tmp/com.example.mertino11.ourapplication"
Success


$ adb shell am start -n "com.example.mertino11.ourapplication/com.example.mertino11.ourapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 2403 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.example.mertino11.ourapplication-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.mertino11.ourapplication, real application class is com.example.mertino11.ourapplication.FireApp.

              [ 12-10 16:35:30.033  1550: 1571 D/         ]
              HostConnection::get() New Host Connection established 0x89bee200, tid 1571
W/System: ClassLoader referenced unknown path: /data/app/com.example.mertino11.ourapplication-1/lib/x86
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
W/System: ClassLoader referenced unknown path: 
W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
I/FA: App measurement is starting up, version: 10084
I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
D/FA: Debug-level message logging enabled
D/FA: AppMeasurement singleton hash: 174613062
I/art: Background sticky concurrent mark sweep GC freed 21982(2MB) AllocSpace objects, 55(1056KB) LOS objects, 34% free, 7MB/11MB, paused 7.752ms total 131.168ms
V/FA: Collection enabled
V/FA: App package, google app id: com.example.mertino11.ourapplication, 1:113712880364:android:c60a6cfc3967db90
I/FA: To enable faster debug mode event logging run:
        adb shell setprop debug.firebase.analytics.app com.example.mertino11.ourapplication
V/FA: Registered activity lifecycle callback
I/FirebaseInitProvider: FirebaseApp initialization successful
V/FA: Using measurement service
V/FA: Connecting to remote service
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
V/FA: onActivityCreated
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 29161
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 2587: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa71b42c0, error=EGL_BAD_MATCH
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 497
V/FA: Using measurement service
V/FA: Connection attempt already in progress
V/FA: Activity paused, time: 29657
I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 3
V/FA: onActivityCreated
V/FA: Activity resumed, time: 30137
E/EGL_emulation: tid 2587: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa71b42c0, error=EGL_BAD_MATCH
V/FA: Inactivity, disconnecting from the service
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
V/FA: Session started, time: 39646
I/FA: Tag Manager is not found and thus will not be used
D/FA: Logging event (FE): _s, Bundle[{_o=auto, _sc=AccountActivity, _si=-1992202464745463440}]
V/FA: Using measurement service
V/FA: Connecting to remote service
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
V/FA: Inactivity, disconnecting from the service

Screenshot of my hierachy of the project itself:

http://i66.tinypic.com/vor192.png

The scenario of the app:

  • Create account firebase Google (manually)
  • Opens app --> Sees login page (where I am stuck at)
  • Logs in with accountdetails of Firebase
  • Goes to AccountActivity page

Note: I am a amateur/beginner with AndroidStudio.

I didn't get question correctly.anyways in your manifest.xml,

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

    <uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".FireApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true">

    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity
        android:name=".AccountActivity"
        android:theme="@style/AppTheme" />
</application>

</manifest>

intent-filter inside the MainActivity means that the MainActivity is the launcher Activity

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

So if you want to launch the AccountActivity first then change to

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mertino11.ourapplication">
    <uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".FireApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true">

    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme"/>

    <activity
        android:name=".AccountActivity"
        android:theme="@style/AppTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

</manifest>

1) you have already login with email and password that reason app going to account activity

2) you can prevent using

FirebaseAuth.getInstance().signOut();

3) more detail FireBase at the bottom

4) thank u

Button logout=(Button) findViewById(R.id.logout)
    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(AccountActivity.this,MainActivity.class));
            finish();
        }
    });

xml

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="logout"
  android:id="@+id/logout"/>

in AccountActivity.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM