Can someone tell me how i do that or someone can change this activity to fragment for me. i have very few experiance in cooding right now, so i can't do this my self.
public class SplashActivity extends AppCompatActivity {
FirebaseAuth auth;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
auth=FirebaseAuth.getInstance ();
user=auth.getCurrentUser ();
new Handler( ).postDelayed (new Runnable () {
@Override
public void run() {
if (user !=null)
{
startActivity ( new Intent( SplashActivity.this,HomeActivity.class ) );
}
else
{
startActivity ( new Intent ( SplashActivity.this,Login.class ) );
}
finish ();
}
},1500 );
}
}
CodePudding user response:
I would start by understanding their lifecycles (activity and fragment). We can then have a good idea of where to move your code from onCreate() in activity to onViewCreated() in Fragment. Also notice that activity extends AppCompatActivity while Fragment extends Fragment class. With those two observations we can now proceed as below. Note the renaming of the layout and class due to the change from activity to class.
class SplashFragment extends Fragment {
private FirebaseAuth auth;
private FirebaseUser user;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
auth = FirebaseAuth.getInstance ();
user = auth.getCurrentUser ();
return inflater.inflate(R.layout.fragment_splash, container, false);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new Handler().postDelayed (new Runnable () {
@Override
public void run() {
//Do your logic here.
}
}, 1500);
}
}
References: Fragment lifecycle: https://developer.android.com/guide/fragments/lifecycle Fragment implementation: https://developer.android.com/guide/fragments/create