简体   繁体   English

Android-单击而不是前进可返回上一页

[英]Android - by clicking instead of going forward is back to the previous page

I use Android Studio, created four new activity each button. 我使用Android Studio,每个按钮创建了四个新活动。 I try every button leads to the next page. 我尝试每个按钮都指向下一页。 After I attach all the buttons I run the app and it really works. 附加所有按钮后,我将运行该应用程序,它确实可以正常工作。 When I press the button on the first page it takes me to the second page, but when I press the button on the second page to go to the third, it takes me back to the first page. 当我按下第一页上的按钮时,将带我进入第二页,但是当我按下第二页上的按钮以转到第三页时,它将带我回到第一页。

first activity: 第一次活动:

 package liranbenzino.kids;
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener()    {
        @Override
        public void onClick(View viewBtnClick) {
            // link to class
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);


        }});
}}

second activity: 第二项活动:

package liranbenzino.kids;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Main2Activity extends AppCompatActivity {

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

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View viewBtnClick1) {
            // link to class
            Intent intent = new Intent(Main2Activity.this,Main3Activity.class);
            startActivity(intent);


        }});
}
}

thied activity: 活动:

package liranbenzino.kids;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Main3Activity extends AppCompatActivity {

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

    findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View viewBtnClick) {
            // link to class
            Intent intent = new Intent(Main3Activity.this,Main4Activity.class);
            startActivity(intent);


        }});
}
}

four activity: 四个活动:

package liranbenzino.kids;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
public class Main4Activity extends AppCompatActivity {

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


    findViewById(R.id.button4).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View viewBtnClick) {
            Toast.makeText(Main4Activity.this,"NAME", Toast.LENGTH_LONG).show();


}});
}}

AndroidMenifest.xml : AndroidMenifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="liranbenzino.kids">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

thank'S 谢谢

Your code is ok 您的代码还可以

How your decide that you back to firstPage when your click 2nd activity button. 当您单击第二活动按钮时,如何决定返回第一页。 your code is ok.. use label in each page. 您的代码还可以。在每个页面中使用标签。 then try ... 然后尝试...

May be your some button label is same and you become confuse in which page you are in.. 可能是您的某些按钮标签相同,并且使您混淆所在的页面。

You can change your button label as First_Page_Button,Second_Page_Button,Third_Page_Button etc to decide in which page you are... 您可以将按钮标签更改为First_Page_Button,Second_Page_Button,Third_Page_Button等,以决定您位于哪个页面中...

in each activity change the INTENT name for example in activity2 change Intent intent = new Intent(Main2Activity.this,Main3Activity.class); 在每个活动中更改INTENT名称,例如在activity2中更改Intent intent = new Intent(Main2Activity.this,Main3Activity.class); startActivity(intent); startActivity(intent); to Intent intent2 = new Intent(Main2Activity.this,Main3Activity.class); to Intent intent2 = new Intent(Main2Activity.this,Main3Activity.class); startActivity(intent2); startActivity(intent2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 系统将我返回到上一页,而不是在没有点击“下一步”或“返回”按钮的情况下进入下一个页面 - System returns me back to the previous page instead of taking to the next one without even clicking 'Next' or 'Back' button 返回上一页时使用 Intent() 而不是 finish() 有什么好处? - What is the advantage of using Intent() instead of finish() when going back to the previous page? 最小化键盘,而不是按回上一步即可返回上一个活动 - Minimize keyboard instead of going back to previous activity on press back Android禁止返回以前的活动 - Android prevent going back to previous activities 返回上一个JPanel - Going back to previous JPanel 返回上一个屏幕 - Going Back to Previous Screens 使用意图返回上一页,而是进入上一页… - Using intent to go back previous page instead go in to the previous page… 如何通过单击android中的上一个按钮导航上一页? - how to navigate the previous page by clicking previous button in android? 回到之前的场景 JavaFX - Going back to a previous scene in JavaFX Spring从POST重定向到GET并让后退按钮返回上一页而不是同一页 - Spring Redirect from POST to GET and have back button go back to previous page instead of same page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM