androidandroid-fragmentshere-apiheremapsheremaps-android-sdk

Heremap AndroidXMapFragment back press handling


I work with HERE map Android SDK and with its AndroidXMapFragment. I add map fragment to my fragment container in the next way:

appActivity.getSupportFragmentManager().beginTransaction()
            .replace(R.id.fragment_container, hereMapFragment, TAG)
            .addToBackStack(TAG)
            .commit();

But when back press action happens, I get an empty fragment, without map and objects in it. So, question is: How I should process back press and should I recreate AndroidXMapFragment manually? In documentation the only thing I found is:

AndroidXMapFragment objects have their own lifecycle, state, and back stack, thus it is unsafe to assume objects returned by a AndroidXMapFragment instance (with the exception of Map) will be available throughout the lifetime of its attached activity.


Solution

  • I found an answer on the question after trying different approaches. So the answer is: If you have to use fragment-based architecture of your app, you should handle back press manually and recreate AndroidXMapFragment and initialize it again with parameters you have. In this case you should use:

    appActivity.getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, searchLocationFragment, TAG)
                    .commit();
    

    without addToBackStack method. And also for handling back press use Activity's override method:

    @Override public void onBackPressed() {
            Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
            AppController.instance(this).onBackPressed(fragment);
    }
    

    But if there are no need to keep fragment-based architecture, I'd recommend to use the latest sdk. There is a MapView, which you can use instead of AndroidXMapFragment.