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



我是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);
}
}

我也尝试使用意图,但根本不起作用。 我只想从DetailActivity回到HomeActivity,请帮助我。

提前致谢

这是正常的,因为您在第一个活动中调用finish(),因此它被销毁(按此顺序调用onPause()onStop()onDestroy()(,它将不再在堆栈上。

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

编辑:正如@ADM评论中所说,您必须像下面这样继续操作才能启动DetailActivity:

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

相关内容

  • 没有找到相关文章

最新更新