如何将活动中的“后退”按钮添加到片段

时间:2019-05-10 09:58:51

标签: android

我正在使用一个片段,因为我使用了导航抽屉菜单。单击“类别”菜单后,将带我进入“类别”片段,在该片段中,我添加了一个浮动操作栏,它将带您进入“添加类别”活动。

由于片段无法在AndroidManifest.xml文件中查看,因此我无法将片段作为父对象。我想要的是从“添加类别”活动中添加“后退按钮”,显示一条吐司消息(如果确定不保存更改则退出。.etc),然后返回“类别”片段。

我尝试使用“ onBackPressed()”和“ getSuppoerActionBar()”来显示祝酒词,但是在运行应用程序时,祝酒词消息将在几秒钟后显示,并返回“ Category”类别。

请帮助。谢谢! 这是我的代码。

CategoriesFragment.java

package com.example.devcash;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class CategoriesFragment extends Fragment {


    public CategoriesFragment() {
        // Required empty public constructor
    }


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

        //add floating action button
        FloatingActionButton categories_fab = view.findViewById(R.id.addcategories_fab);
        categories_fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // when add fab is pressed, go to add product activity
                Intent addprod = new Intent(getActivity(), AddCategoryActivity.class);
                startActivity(addprod);
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getActivity().setTitle("Categories");
    }
}

AddCategoryActivity.java

package com.example.devcash;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class AddCategoryActivity extends AppCompatActivity {

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Unsaved changes");
        builder.setMessage("Are you sure you want to leave without saving changes?");
        builder.setPositiveButton("LEAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                AddCategoryActivity.super.onBackPressed();
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        builder.show();


        super.onBackPressed();
    }
}

3 个答案:

答案 0 :(得分:1)

您需要从onBackPressed()中删除以下代码,因为它正在调用超类方法并最终破坏了活动。

super.onBackPressed();

要向后处理工具栏,请添加以下代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // todo: goto back activity from here
            onBackPressed();
        default:
            return super.onOptionsItemSelected(item);
    }
}

答案 1 :(得分:1)

您的AddCategoryActivity应该是这样的

public class AddCategoryActivity extends AppCompatActivity {

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onBackPressed() {
        quitActivity();
    } 

    private void quitActivity() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Unsaved changes");
        builder.setMessage("Are you sure you want to leave without saving changes?");
        builder.setPositiveButton("LEAVE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            quitActivity();
            return true;
        } else
            return super.onOptionsItemSelected(item);
    }
}

答案 2 :(得分:0)

您可以使用EventBus。

class Activity{

  fun backToFragment(){
    EventBus.postEvent(BackToFragmentEvent())
  }
}

class HomeActivity{
  EventBus.register{
    fragmentmanager.replace(CategoriesFragment)
  }
}