In Kotlin ..How I can return from fragment to MainActivity

南笙酒味 提交于 2019-12-11 05:06:38

问题


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()
    }
}

My first problem is:

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

super.onBackPressed()

not working ..why

My second problem is:

after else If I need to use

getActivity().onBackpressed()

instead of the old one..

Thanks All


回答1:


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.

To navigate back to the previous Fragment:

activity?.fragmentManager?.popBackStack()

To navigate to the previous activity:

activity?.finish()

or

onBackPressed()

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

super.onBackPressed()

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)

 }

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.

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.




回答2:


For your first question as far as I can understand you should use

val activity = activity as MainActivity
activity.onBackPressed()

because super for your fragment is not MainActivity.



来源:https://stackoverflow.com/questions/49121493/in-kotlin-how-i-can-return-from-fragment-to-mainactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!