如何将活动转换为Android Studio中的片段
有人可以告诉我我是怎么做到的,或者有人可以将这项活动更改为我的碎片。 我现在很少有经验来旋转,所以我不能这样做。
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 );
}
}
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 );
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将首先了解它们的生命周期(活动和碎片)。然后,我们可以很好地了解将代码从活动中的otCreate()转移到片段中的ot otewiewCreated()。还要注意,活动扩展了应用程序围位,而碎片扩展了碎片类。通过这两个观察,我们现在可以按照下面进行。请注意,由于从活动到类别的变化,布局和类的重命名。
参考:
片段生命周期: https://developer.android.com/guide.com/guide/guide/fragments/fragments/lifecycle
片段实现: https://developer.android.com/guide.com/guide/fragments/fragments/create/create
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.
References:
Fragment lifecycle: https://developer.android.com/guide/fragments/lifecycle
Fragment implementation: https://developer.android.com/guide/fragments/create