单击片段的操作栏后退按钮时,重定向到主要活动

时间:2018-06-06 08:12:59

标签: android android-fragments android-navigation-drawer

这个项目与地图有关。在这里,登录后,它被重定向到地图活动。我认为地图活动是这里的主要活动。有导航抽屉和每个选项(配置文件,设置,帮助......),我使用片段。

在导航栏中选择配置文件选项后,它将转到配置文件片段。现在,当我按下片段的 中的后退按钮时,我需要返回主要活动(地图活动)。

地图活动不是导航抽屉中的选项。

映射Activity.java

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    mSearchText = (AutoCompleteTextView) findViewById(R.id.input_search);
    mGps = (ImageView) findViewById(R.id.ic_gps);
    mInfo=(ImageView) findViewById(R.id.place_info);
    btnFindPath = (Button) findViewById(R.id.btnFindPath);

    mDrawerlayout= (DrawerLayout) findViewById(R.id.drawer);
    mToggle= new ActionBarDrawerToggle(this,mDrawerlayout,R.string.open,R.string.close);
    mDrawerlayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv);
    setupDrawerContent(nvDrawer);

    getLocationPermission();
}


public void selectIterDrawer(MenuItem menuItem){
    Fragment myFragment=null;
    Class fragmentClass;
    switch (menuItem.getItemId()){
        case R.id.profile:
            fragmentClass = Profile.class;
            break;
        case R.id.search:
            fragmentClass = Search.class;
            break;
        case R.id.help:
            fragmentClass = Help.class;
            break;
        case R.id.settings:
            fragmentClass = Settings.class;
            break;
        case R.id.logout:
            fragmentClass = Logout.class;
            break;
        default:
            fragmentClass = Profile.class;
    }

    try {
        myFragment = (Fragment) fragmentClass.newInstance();
    }
    catch (Exception e){
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());
    mToggle.setDrawerIndicatorEnabled(false);
    mDrawerlayout.closeDrawers();
}

private void setupDrawerContent(NavigationView navigationView){
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            selectIterDrawer(item);
            return true;
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    if (mToggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);
}

profile.java(片段)

   public static Profile newInstance(String param1, String param2) {
    Profile fragment = new Profile();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);

    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_profile, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

1 个答案:

答案 0 :(得分:0)

我认为您必须再次回拨主要活动以进行后退按钮..

定义一个启动活动的方法,并在onOptionItemSelected()方法中调用它。像..

public void onBackPressed(){
   Intent  i=new Intent(this,Mainactivity.class)
   startActivity(intent);
   finish();
} 

public boolean onOptionItemSelected(MenuItem item){
   if(item.getItemID()==android.R.id.home){
        onBackPressed();
        return true;
   }
   return super.onOptionsItemSelected(item);
}

我不确定,这可能会有所帮助......