点击登录按钮显示加载器后,如何在其隐藏时附加已创建Fragment
How to Attach Your Existing Fragment After Loader Hides in MainActivity
Hey there! Sounds like you're almost there—you've got your Activity, Fragment, and the Handler/Runnable set up for the loader logic. Let's hook up the Fragment attachment so it runs right after the loader disappears. Here's how to do it:
Step 1: Integrate Fragment Transaction into Your Runnable
Your Runnable is where the post-loader logic lives, so we'll add the Fragment transaction code directly inside its run() method. Make sure you're working with the correct FragmentManager and container view from your layout.
Here's the updated code snippet for your MainActivity:
// Initialize your Handler with the main looper to ensure UI operations run on the main thread handler = new Handler(Looper.getMainLooper()); runnable = new Runnable() { @Override public void run() { // First, hide your loader (replace progressBar with your actual loader view) progressBar.setVisibility(View.GONE); // Check if the Activity is still active to avoid crashes if (!isFinishing() && !isDestroyed()) { // Get the FragmentManager (use getSupportFragmentManager() for AndroidX/AppCompatActivity) FragmentManager fragmentManager = getSupportFragmentManager(); // Start a Fragment transaction FragmentTransaction transaction = fragmentManager.beginTransaction(); // Instantiate your pre-created Fragment (replace with your actual Fragment class name) YourPreCreatedFragment targetFragment = new YourPreCreatedFragment(); // Optional: Pass arguments to the Fragment if needed Bundle fragmentArgs = new Bundle(); fragmentArgs.putString("USER_ID", "some_user_id"); // Example argument targetFragment.setArguments(fragmentArgs); // Replace the container view with your Fragment (replace R.id.fragment_container with your layout's container ID) transaction.replace(R.id.fragment_container, targetFragment); // Optional: Add to back stack so users can navigate back with the back button transaction.addToBackStack(null); // Commit the transaction to apply changes transaction.commit(); } } };
Key Notes to Keep in Mind
- Fragment Container: Make sure your MainActivity's layout has a container view (like a
FrameLayout) with the ID you reference intransaction.replace()—this is where the Fragment will be attached. - AndroidX vs. Native Fragments: If you're using AndroidX Fragments (recommended), use
getSupportFragmentManager(). For older native Fragments, usegetFragmentManager(). - Activity State Check: The
!isFinishing() && !isDestroyed()check prevents trying to commit a transaction when the Activity is already shutting down, which avoids crashes. - Back Stack: Adding
addToBackStack(null)lets users press the back button to return to the login screen (or previous state) instead of exiting the app. Remove this line if you don't want that behavior.
Triggering the Runnable
Don't forget to post your Runnable at the right time—after your login logic completes and you're ready to hide the loader. For example:
// When login is done and you want to show the loader then attach the Fragment progressBar.setVisibility(View.VISIBLE); handler.postDelayed(runnable, 2000); // Simulate loader delay; replace with your actual async logic completion
内容的提问来源于stack exchange,提问作者Nasir ALAM
相关产品推荐
相关产品推荐

