Skip to main content

Command Palette

Search for a command to run...

Android Generic RecyclerView Adapter

Updated
•2 min read•View as Markdown
U

Experienced software engineer with demonstrated history of working in telecommunications industry along with many other sectors like education, e-commerce, healthcare etc. Skilled in Android SDK, Kotlin, SQLite, Java, MVP, MVVM,REST, JSON JavaScript. Strong engineering professional with a bachelor of engineering focused in computer engineering. Solid understanding of the full mobile development life cycle.

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class FragmentRemovalHelper {

    public static void removeAllFragmentsNotInBackStack(FragmentManager fragmentManager, Runnable postRemovalAction) {
        CountDownLatch latch = new CountDownLatch(1);

        FragmentManager.OnBackStackChangedListener listener = new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                latch.countDown();
            }
        };

        fragmentManager.addOnBackStackChangedListener(listener);

        fragmentManager.executePendingTransactions();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        List<Fragment> fragments = fragmentManager.getFragments();
        for (Fragment fragment : fragments) {
            if (fragment != null && !isInBackStack(fragmentManager, fragment)) {
                fragmentTransaction.remove(fragment);
            }
        }

        fragmentTransaction.commit();

        new Thread(() -> {
            try {
                // Wait until back stack change is detected
                latch.await();
                // Run the post-removal action
                if (postRemovalAction != null) {
                    postRemovalAction.run();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // Cleanup listener
                fragmentManager.removeOnBackStackChangedListener(listener);
            }
        }).start();
    }

    private static boolean isInBackStack(FragmentManager fragmentManager, Fragment fragment) {
        for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
            FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(i);
            if (fragment.getTag() != null && fragment.getTag().equals(backStackEntry.getName())) {
                return true;
            }
        }
        return false;
    }
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentRemovalHelper.removeAllFragmentsNotInBackStack(fragmentManager, () -> {
    // Perform your action here after all fragments are removed
    runOnUiThread(() -> {
        // Any UI updates need to be run on the UI thread
        Toast.makeText(this, "All non-backstack fragments are removed", Toast.LENGTH_SHORT).show();
        // You can also start a new fragment transaction or other actions here
    });
});
<resources>
    <declare-styleable name="StepProgressBar">
        <attr name="totalSteps" format="integer" />
        <attr name="currentStep" format="integer" />
        <attr name="stepRadius" format="dimension" />
        <attr name="stepSpacing" format="dimension" />
        <attr name="completedStepColor" format="color" />
        <attr name="incompleteStepColor" format="color" />
        <attr name="connectorColor" format="color" />
    </declare-styleable>
</resources>
<your_package_name.StepProgressBar
    android:id="@+id/stepProgressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:totalSteps="5"
    app:currentStep="2"
    app:stepRadius="20dp"
    app:stepSpacing="40dp"
    app:completedStepColor="#4CAF50"
    app:incompleteStepColor="#BDBDBD"
    app:connectorColor="#616161"/>
#!/bin/bash

# Specify the path to the file or directory
file_or_directory="/path/to/your/file_or_directory"

# Identify the process using the file or directory
pid=$(fuser -m "$file_or_directory")

if [ -n "$pid" ]; then
    echo "Process $pid is using $file_or_directory. Killing the process..."

    # Kill the process forcefully
    kill -9 "$pid"

    echo "Process killed."
else
    echo "No process is using $file_or_directory."
fi

# Retry the deletion
echo "Attempting to remove $file_or_directory..."
rm -r "$file_or_directory"

# Check the result of the removal
if [ $? -eq 0 ]; then
    echo "Removal successful."
else
    echo "Error: Unable to remove $file_or_directory."
fi
M

nice.