Handling back button in Android Navigation Component

Newest Update – April 25th, 2019 New release androidx.activity ver. 1.0.0-alpha07 brings some changes More explanations in android official guide: Provide custom back navigation Example: public class MyFragment extends Fragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // This callback will only be called when MyFragment is at least Started. OnBackPressedCallback callback = … Read more

android.content.Context.getPackageName()’ on a null object reference

I have found the mistake what I did. We need to get the activity instance from the override method OnAttach() For example, public MainActivity activity; @Override public void onAttach(Activity activity){ this.activity = activity; } Then pass the activity as context as following. Intent mIntent = new Intent(activity, MusicHome.class);

Android – save/restore fragment state

When a fragment is moved to the backstack, it isn’t destroyed. All the instance variables remain there. So this is the place to save your data. In onActivityCreated you check the following conditions: Is the bundle != null? If yes, that’s where the data is saved (probably orientation change). Is there data saved in instance … Read more

tech