简体   繁体   中英

back button exits app instead of going to previous activity

I have a number of activities setup from registration to login etc but when I click the back button on my android phone, it exits the app whereas I would like it to go to the previous activity. I have tried backpressed() but doesn't seem to work for me. At this stage, I won't be implementing a back arrow via the toolbar.

Could anyone assist me please? Code is below for one of my activities.

 <?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:background="#FFFFFF"
android:fitsSystemWindows="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@drawable/shape_gradient_orange"
        android:orientation="vertical">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:src="@drawable/search_icon" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:text="@string/app_full_name"
            android:textColor="#FAFAFA"
            android:textSize="20sp" />

    </LinearLayout>

    <android.support.v7.widget.CardView
        android:layout_width="350dp"
        android:layout_height="220dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="160dp"
        android:background="@drawable/border"
        android:id="@+id/cardView">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/logIn"
                android:textSize="20dp" />



            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <EditText
                    android:id="@+id/employerEmailAddress"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="15dp"
                    android:background="#FAFAFA"
                    android:singleLine="true"
                    android:drawableLeft="@mipmap/email_icon"
                    android:drawablePadding="10dp"
                    android:hint="@string/employerEmailAddress"
                    android:maxLines="1"
                    android:textSize="15sp" />

                <EditText
                    android:id="@+id/employerPassword"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:layout_gravity="center"
                    android:inputType="textPassword"
                    android:layout_marginTop="15dp"
                    android:singleLine="true"
                    android:background="#FAFAFA"
                    android:drawableLeft="@mipmap/password_icon"
                    android:drawablePadding="10dp"
                    android:hint="@string/employerPassword"
                    android:maxLines="1"
                    android:textSize="15sp" />


            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_below="@+id/cardView"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button"
            android:layout_width="200dp"
            android:layout_marginTop="10dp"
            android:textColor="#FAFAFA"
            android:layout_height="wrap_content"
            android:text="Log in"
            android:background="@drawable/shape_gradient_orange"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/haveAccount"
            android:textColor="#999"
            android:padding="10dp"
            android:text="@string/signUpMessage"/>
    </LinearLayout>


</RelativeLayout>
</LinearLayout>

Here is my java code - very simple activity for the example.

package com.example.dennis.discover;

 import android.content.Intent;
 import android.nfc.Tag;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.TextView;

 public class EmployerLogIn extends AppCompatActivity {
private TextView haveAccount;

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


    haveAccount = (TextView) findViewById(R.id.haveAccount);

    haveAccount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent employerSignUp = new Intent(EmployerLogIn.this, 
   EmployerSignUp.class);
            startActivity(employerSignUp);
            finish();
            }
        });

     }
 }

startActivity(employerSignUp);之后删除finish() startActivity(employerSignUp);

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