Code Monkey home page Code Monkey logo

activitylifecycle's Introduction

ActivityLifecycle

目的:

  • 在任意位置监听Activity的生命周期
  • 在任意位置向Activity中新增成员变量(扩展变量)
  • 在任意位置请求权限但无需在Activity中重写 onRequestPermissionsResult()方法

使用方法:

build.gradle配置:

dependencies {
    implementation 'com.eshel.lib:ActivityLifecycle:1.1'
}
  • 监听生命周期:
public class DemoView extends View{
    
    public DemoView(Context context){
        super(context);
        ActivityHelper.registerActivityLifecycle(ActivityHelper.getActivity(getContext()), new ActivityLifecycleAdapter() {
            @Override
            public void onActivityDestroyed(Activity activity) {
                super.onActivityDestroyed(activity);
            }

            @Override
            public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
                super.onActivityResult(activity, requestCode, resultCode, data);
            }
        });
    }
}
  • 请求权限
public class DemoView extends View{
        
    public DemoView(Context context){
        super(context);
        Activity act = ActivityHelper.getActivity(v.getContext());
        ActivityHelper.registerActivityLifecycle(act, new ActivityLifecycleAdapter() {
            @Override
            public void onRequestPermissionsResult(Activity activity, int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
                String result = "onRequestPermissionsResult() called with: activity = [" + activity + "], requestCode = [" + requestCode + "], permissions = [" + permissions + "], grantResults = [" + grantResults + "]";
                Log.i(TAG, result);
            }
        });
        ActivityHelper.requestPermissions(act, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2000);
        //无需在Activity中重写 onRequestPermissionsResult 方法
    }
}
  • 向Activity添加扩展变量
public class DemoView extends View{
        
    public DemoView(Context context){
        super(context);
        Activity act = ActivityHelper.getActivity(v.getContext());
        ActivityHelper.set(act, "key", "hahahaha");
        String hahaha = ActivityHelper.get(act, "key", String.class);
    }
}

详细使用方法请参考DemoView

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.