android what to use instead of onRestart() in a fragment
Asked Answered
A

5

3

I'm dealing with .setVisibility() of a view, inside my main fragment at app start. So what I want is that the view is invisible on app start (for this i set INVISIBLE inside onCreateView) and to be visible when I come back to my fragment from other activities while the app is open (and for this I tried to use onRestart() to set view VISIBLE but it cannot resolve onRestart method) is onRestart deprecated or? thanks

EDIT: for all the answers below suggesting to use an onResume (and whom gave a -1), onResume doesn't work as onRestart at all, cause is being called right after onCreateView.

Airlie answered 27/1, 2016 at 14:4 Comment(2)
hi m4tt i had the same problem, did u get the answer for that?Bak
its been a while, but from what i recall, the correct answer was the edit of the reply below, since his first answer wasn't on point. still i had a -2 and he got upvoted cause nobody understood the issue. after i made him notice it he posted the boolean thing which i think worked.Airlie
M
19

Fragments don't have onRestart(). It's only for Activities.

See the lifecycle of fragments below

enter image description here

I suppose you're looking for onResume() instead


Use a boolean flag to check whether or not you're returning to the Fragment:

private boolean firstVisit;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    //other stuff
    firstVisit = true;
}

@Override
public void onResume() {
    //other stuff
    if (firstVisit) {
        //do stuff for first visit only

        firstVisit = false;
    }
}
Morna answered 27/1, 2016 at 14:10 Comment(3)
The problem is onResume is being read also on app start right after onCreate()Airlie
@Airlie use a bool to check whether you're in it the 1st time or not (the latter means you are returning to it)Morna
@TimCastelijns ok, so your +2 answer looks incomplete, my -1 question looks due instead, cause I need more than an onResume to achieve onRestart resultAirlie
H
0

You need to use the onResume() callback method, if you would like to detect when the fragment is visible again

Hildick answered 27/1, 2016 at 14:11 Comment(1)
onResume doesn't work as onRestart at all, cause is being called right after onCreateViewAirlie
E
0

Fragment life cycle doesn't have onRestart() method. You could use onPause() and onResume() as per your requirement.

Further reading : Fragments

Espagnole answered 27/1, 2016 at 14:16 Comment(1)
onResume doesn't work as onRestart at all, cause is being called right after onCreateViewAirlie
P
0

You can use either onStart() or onResume() if you want to load things when returning to the fragment.

Perquisite answered 27/1, 2016 at 14:40 Comment(2)
Sadly fragments don't have onrestartMorna
onResume doesn't work as onRestart at all, cause is being called right after onCreateViewAirlie
F
0

You can use onRestart() on the activity, making it call whatever method you want on the fragment by making use of getFragmentManager().findFragmentById(R.id.your_fragment). When a fragment gets restarted its underlying activity got restarted so its onRestart() method was called.

Fidel answered 17/12, 2017 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.