我正在尝试覆盖后退按钮,因为它在我按下时关闭了我的应用程序,我有不同的片段:
我有我的主要活动:它管理我的片段(因为有一个导航抽屉)。
我发现了很多关于此的帖子,但我无法实现它们:
以片段 B 为例:
@Override
public void onBackPressed(){
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragmentb); // get the fragment that is currently loaded in placeholder
Object tag = f.getTag();
// do handling with help of tag here
// call super method
super.onBackPressed();
}
Run Code Online (Sandbox Code Playgroud)
它说无法解析 onBackPressed() 和 getSupportFragmentManager(),我想我没有正确使用该方法,那怎么办?
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import …Run Code Online (Sandbox Code Playgroud)