在Kotlin中..我如何从片段返回MainActivity [英] In Kotlin ..How I can return from fragment to MainActivity

查看:554
本文介绍了在Kotlin中..我如何从片段返回MainActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有MainActivity和许多片段. 我尝试以下代码通过以下方式从片段返回MainActivity onBackpressed()方法

in my program I have MainActivity and many fragments.. I try the following code to return from fragment to MainActivity by onBackpressed() method

override fun onBackPressed() {

    if(drawer_layout.isDrawerOpen(GravityCompat.START)) {
        drawer_layout.closeDrawer(GravityCompat.START)
    }
    else  if (fragment != null) {
    val intent = Intent(applicationContext, MainActivity::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
    startActivity(intent)

     }


    else {
       super.onBackPressed()
    }
}

我的第一个问题是:

它与Drawer配合良好,并且还打开MainActivity 但是程序没有关闭..这些主要的内容

it working good with Drawer and also open MainActivity but program not closed ..these main that

super.onBackPressed()

不起作用..为什么

我的第二个问题是:

在其他地方,如果我需要使用

after else If I need to use

getActivity().onBackpressed()

代替旧的..

谢谢所有

推荐答案

活动通过onBackPressed()导航到活动".片段必须驻留在活动中(它们基本上是子活动),因此通过super.onBackPressed()从片段导航到活动是没有意义的.您应该从片段"导航到片段",或者如果您放弃片段",则从活动"转向活动".

Activities navigate to Activities via onBackPressed(). Fragments have to reside in an Activity (They are basically sub-Activities), so it doesn't make sense to navigate from a Fragment to an Activity via super.onBackPressed(). You should be navigating from Fragment to Fragment, or if you forgo Fragments then Activity to Activity.

要导航回上一个片段:

activity?.fragmentManager?.popBackStack()

要导航到上一个活动:

activity?.finish()

onBackPressed()

,或者,如果您已覆盖onBackPressed()方法,则从活动中获取

or, from the activity if you have overriden the onBackPressed() method:

super.onBackPressed()

在没有更多代码上下文的情况下,我也无法说出为什么最终的else语句从未被调用过.似乎您的if else语句存在一个错误,因为super.onBackPressed()将提供关闭您所从事的任何活动的预期结果(MainActivity?).

Without more context to your code I can't say either why it seems your final else statement is never called. It seems like you've got a bug with your if else statement as super.onBackPressed() would provide the desired result of closing whatever activity you were in (MainActivity?).

else  if (fragment != null) {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(intent)

 }

我的猜测是它与您创建MainActivity的另一个实例有关.第一次按下时,您合上了抽屉.在第二个上,您创建另一个MainActivity实例并导航到它.在第三个实例上,即使调用super.onBackPressed(),它也会导航回到MainActivity的第一个实例,除非您专门分配了片段,否则fragment可能永远不会为null,因此在第四个实例上,您将创建另一个MainActivity实例并导航至该实例. .这是一个永远不会从第一个MainActivity返回的循环.

My guess is it has something to do with you creating another instance of MainActivity. On first back pressed you close the drawer. On the second you create another instance of MainActivity and navigate to it. On the third, even if super.onBackPressed() gets called it will navigate back to the first instance of MainActivity where fragment will probably never be null unless you're specifically assigning such, so on the fourth you create another instance of MainActivity and navigate to it. This is a loop that will never navigate back from the first MainActivity.

建议:但是,您正在显示MainActivity,请将其转换为Fragment并进行相应处理.另一种方法是代替创建MainActivity的另一个实例,隐藏fragmentView并显示MainActivity视图.我不建议将您的fragment设置为null,因为fragmentManager可能会抛出并出错,因此您还应该更改if else逻辑以检查其他内容.说fragment.view.visibility == View.Visible,如果您遵循所述路线.

Suggestions: However you're displaying MainActivity, convert it to a Fragment and handle it accordingly. Another approach is instead of creating another instance of MainActivity hide the fragmentView and show the MainActivity view. I don't suggest setting your fragment to null as the fragmentManager may throw and error, so you should also change your if else logic to check for something else. Say maybe fragment.view.visibility == View.Visible if you go the route described.

这篇关于在Kotlin中..我如何从片段返回MainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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