后退按钮正在关闭应用程序,而不是返回到父活动

时间:2018-05-15 13:00:06

标签: android onbackpressed

我是Android开发的新手。

按下“返回”按钮时,应将用户重定向到HomeActivity,但在按“返回”按钮时,应用程序将被关闭。

我的代码中遗漏了什么?

我在下面发布了我的主要活动:

package com.iridh.collegeapp;

import android.content.Intent;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.view.menu.ExpandedMenuView;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


    DrawerLayout navDrawer;
    private ActionBarDrawerToggle navToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        navDrawer= (DrawerLayout)findViewById(R.id.navDrawer);
        navToggle=new ActionBarDrawerToggle(this,navDrawer,toolbar,R.string.open,R.string.close);
        navDrawer.addDrawerListener(navToggle);
        Button btn1=(Button)findViewById(R.id.btn_more);
        Button btn2=(Button)findViewById(R.id.btn_more2);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_detail);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setContentView(R.layout.activity_detail);
            }
        });
        navToggle.syncState();



        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener(){
            @Override
            public void  onClick(View v) {
                startActivity(new Intent(getApplicationContext(),HomeActivity.class));
                finish();
            }

            public void onConfigurationChanged(Configuration newConfig) {
                navToggle.onConfigurationChanged(newConfig);
                navToggle.onConfigurationChanged(newConfig);
            }
        });


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    public boolean onOptionItemSelected(MenuItem item){
        if (navToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {

            case R.id.menu_home: {

                break;
            }
            case R.id.menu_governance: {
              setContentView(R.layout.activity_detail);

            }
            case R.id.menu_campus: {
                setContentView(R.layout.activity_campus);
                break;


            }
            case R.id.menu_department: {
                setContentView(R.layout.activity_departments);
                break;

            }
            case R.id.menu_admission: {
                setContentView(R.layout.activity_admission);
                break;


            }
            case R.id.menu_facilities: {
                setContentView(R.layout.activity_facilities);
                break;


            }
            case R.id.menu_gallery: {
                setContentView(R.layout.activity_gallery);
                break;

            }
            case R.id.menu_downloads: {
                setContentView(R.layout.activity_downloads);
                break;
            }
            case R.id.menu_placements: {
                setContentView(R.layout.activity_placements);
                break;

            }
            case R.id.menu_contact: {
                setContentView(R.layout.activity_contact);
                break;


            }

        }

        navDrawer.closeDrawer(GravityCompat.START);
        return true;
    }


};

这是第二项活动

package com.iridh.collegeapp;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class DetailActivity extends HomeActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Button b3 = findViewById(R.id.btn_back);
        b3.setOnClickListener((View.OnClickListener) this);
        }


    public void  onClick(View v) {

        finish();  //this button i created to get back back doesnt work 
    }
    public void onBackPressed()
    {
        setContentView(R.layout.activity_home);
    }


}

我也尝试过使用intent,但根本没用。 我只想从DetailActivity回到HomeActivity,请帮助我。

提前致谢

1 个答案:

答案 0 :(得分:0)

这是正常的,因为您在第一个活动中调用了finish(),因此它被销毁(按此顺序调用onPause()onStop()onDestroy())并且它不会更长时间在堆栈上。

因此,如果您删除第一个活动中的finish(),您可以返回它。

编辑:同样@ADM在评论中说,您必须按照以下步骤启动DetailActivity:

startActivity(new Intent(HomeActivity.this,DetailActivity.class));