添加后退按钮并覆盖onBackPressed [英] Adding back button and override onBackPressed

查看:95
本文介绍了添加后退按钮并覆盖onBackPressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在活动中添加后退箭头,并覆盖按下的后退按钮以仅执行设备上的后退"功能,即行按钮.我可以在不对活动B实施父活动A的情况下执行此操作吗? >

I want to add back arrow in my activity,and override back pressed to just doing "back" function,line button on my device.Can I do this without implementing parent activityA to my Activity B.Tried to use

getActionBar().setDisplayHomeAsUpEnabled(true);

但是android告诉我,这段代码可能会产生NPE,并且当我尝试设置主题Theme.AppCompat android studio时出现错误,提示我没有解析符号. 我的活动代码:

but android told me that this code may produce NPE,and when I try to set theme Theme.AppCompat android studio give me error that no resolve symbol. my Activity Code:

public class Activity_videoplayer extends ActionBarActivity {
    MyVideoView myVideoView;
    VideoView videoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ctivity_videoplayer);
        Intent intent = getIntent();
        String video_url = intent.getExtras().getString("video_url");
       videoView = (VideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse(video_url));
        videoView.requestFocus();
        videoView.start();

    }
    public void onBackPressed(){
        super.onBackPressed();

    }
}

推荐答案

确保在清单中设置您的活动或片段

make sure to set your activity or fragment in manifest

 assert getSupportActionBar() != null;
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

并在清单文件中为您的片段或活动设置此代码

and set this code in your manifest file for your fragment or activity

<activity android:name=".yourCurrentFragmentOrActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".whichActivityYouWantToGoBack" />
</activity>

仅当您为其设置了父级活动时,您的后退箭头才起作用. 而且不需要onBackPressed事件

your back arrow only work when you set parent activity for it. and there is no need for onBackPressed Event

和您的第二个答案:-

您扩展了actionBarActivity,这意味着您的目标sdk版本为21,如果您想使用appCompat主题,则必须扩展AppCompatActivity, 并且目标sdk版本应为22或23.如果您的平台插件和sdktool已完全升级,则您无需自己设置任何内容,它将自动设置所有内容.因此,只需升级您的SDK和平台插件即可. 您可以检查我的

you extend actionBarActivity that meant your target sdk version is 21, if you want to use appCompat theme then you have to extend AppCompatActivity, and for that your target sdk version should be 22 or 23. and if your platform plugin and sdktool is fully upgraded then you dont have to set anything on your own, it will automatically set everything. so just upgrade your sdk and platform plugin. you can check mine

:

及其最新版本

这篇关于添加后退按钮并覆盖onBackPressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆