Issue
I am basically trying to navigate to other fragment after waiting 5 seconds.When I do the navigation process in my onViewCreated it works but when i do it in onFinish it gives me the error below.
I tried to keep View object as a global variable but it didn't work either.
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ui, PID: 17838 java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{8dcd038 V.E...... .......D 0,0-1080,1908} does not have a NavController set at androidx.navigation.Navigation.findNavController(Navigation.java:84) at com.example.ui.sign_in.LoadingFragment$1.onFinish(LoadingFragment.java:39) at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6810) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) I/Process: Sending signal. PID: 17838 SIG: 9
** private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_loading, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
this.view = view;
new CountDownTimer(5000, 1000) { //Wait 5 seconds
public void onTick(long millisUntilFinished) {
}
public void onFinish() { // Then navigate to other page.
navigate();
}
}.start();
super.onViewCreated(view, savedInstanceState);
}
private void navigate() {
Navigation.findNavController(this.view).navigate(LoadingFragmentDirections.actionLoadingFragmentToSignInPagerFragment());
}**
Solution
Solution 1)
You need to check if fragment is visible or not anymore before onFinish()
get called
Add a boolean , initialize it in onResume()
and make it false in onPause()
like this
private boolean isFragmentVisible=false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_loading, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
this.view = view;
new CountDownTimer(5000, 1000) { //Wait 5 seconds
public void onTick(long millisUntilFinished) {
}
public void onFinish() { // Then navigate to other page.
//check if fragment is visible or not
if(isFragmentVisible){
navigate();
}
}
}.start();
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
isFragmentVisible=true;
}
@Override
public void onPause() {
super.onPause();
isFragmentVisible=false;
}
private void navigate() {
Navigation.findNavController(this.view).navigate(LoadingFragmentDirections.actionLoadingFragmentToSignInPagerFragment());
}**
Solution 2) You need to declare an instance of CountDownTimer and initialize it once and make it null when fragment is no more visible
Answered By - Quick learner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.