如何将活动转换为Android Studio中的片段

发布于 2025-01-29 07:40:25 字数 835 浏览 2 评论 0原文

有人可以告诉我我是怎么做到的,或者有人可以将这项活动更改为我的碎片。 我现在很少有经验来旋转,所以我不能这样做。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬心 2025-02-05 07:40:25

我将首先了解它们的生命周期(活动和碎片)。然后,我们可以很好地了解将代码从活动中的otCreate()转移到片段中的ot otewiewCreated()。还要注意,活动扩展了应用程序围位,而碎片扩展了碎片类。通过这两个观察,我们现在可以按照下面进行。请注意,由于从活动到类别的变化,布局和类的重命名。

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);
    }
    
}

参考:
片段生命周期: 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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文