0

After restarting my application,

getActivity() returns null in my mainFragment.

Workflow:

When I launch my app, I first start with loginActivity (if not login already), then launch mainActivity.

I got mainFragment attached to my mainActivity. If I unlog() (bringing me back to loginActivity and finish the mainActivity), and start again the same worflow, getActivity() from my fragment return null.

When I see the log, when I first launch the app, I can see that my fragment is detached from the activity when I use the unlog() function

I found several similar questions on SO:

saying this : then you should not use parent(no longer) Activity in the fragment

I would like launch a new activity from my fragment when I click on a button, but how can I do this without using parent Activity in the fragment? (new Intent(this.getActivity(), newActivity.class); returning NPE cause of the null value from getActivity)

Community
  • 1
  • 1
gamerounet
  • 279
  • 4
  • 22
  • How many fragments do you have in the Main Activity ? – OBX Feb 03 '17 at 18:04
  • I got 5 or 6 fragment, and a fragment manager in my mainActivity. But in my case, I only call 1 fragment, the mainFragment `FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.main_content, fragment); transaction.addToBackStack(null); transaction.commit();` – gamerounet Feb 03 '17 at 18:13
  • So now the app crashes while clicking the button which launches another activity using intent? – OBX Feb 03 '17 at 18:15
  • When I'm on my main fragment and I clicked on my button to launch the game, (in a new activity, gameActivity). It never crash when it's the first launch, only when I come back in mainActivity after unlog – gamerounet Feb 06 '17 at 08:58

0 Answers0