A method for public tools to monitor the end of Activity life cycle and automatically recycle

Keywords: Fragment simulator

I've seen Glide's source code before, but the code package of Glide is strict and cannot be solved. There is a business scenario that may lead to memory leakage. I mentioned this method, but it felt a little complicated, so I didn't do it. In the afternoon, I spent a little time to sort out the following ideas again, and found that it was much simpler than expected. Let's talk about the principle first:
The life cycle of Fragemnt in Activity will be recalled. Then we put an empty Fragemnt in, and we can do what we want to do in the ondestore of Fragemnt (eg: recycle and so on)

Here is the code for demo

45 lines of code, specifically for a single example. Test available on pixel, mumu simulator

public class LifeFunction {

    private static LifeFunction sInstance = new LifeFunction();
    public static LifeFunction getInstance(){
        return sInstance;
    }

    private LifeFunction(){
    }

    private SimpleCallback mCallback = null;
    private String TAG = LifeFunction.class.getSimpleName();

    public void register(Context withContext,SimpleCallback callback){
        this.mCallback = callback;
        if(withContext instanceof Activity){
            FragmentManager fm = ((Activity) withContext).getFragmentManager();
            RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(TAG);
            if (current == null) {
                current = new RequestManagerFragment();
                fm.beginTransaction().add(current, TAG).commitAllowingStateLoss();
            }
        }
    }

    public void unregister(){
        this.mCallback = null;
    }

    public void call(){
        if(this.mCallback != null)this.mCallback.onCall();
    }

    public interface SimpleCallback{
        void onCall();
    }

    public static class RequestManagerFragment extends Fragment{
        @Override
        public void onDestroy(){
            super.onDestroy();
            Log.e("TAG","unregister fragment");
            LifeFunction.getInstance().unregister();
        }
    }
}

The simple test code is as follows

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(intent);

        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LifeFunction.getInstance().call();
            }
        });
    }

}
public class SecondActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LifeFunction.getInstance().register(SecondActivity.this, new LifeFunction.SimpleCallback() {
            @Override
            public void onCall() {
                Log.e("TAG","onCall");
                Toast.makeText(SecondActivity.this, "test", Toast.LENGTH_SHORT).show();
            }
        });

        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LifeFunction.getInstance().call();
            }
        });
    }
}

Posted by discosuperfly on Fri, 20 Mar 2020 10:26:32 -0700