美文网首页
分享一些传值的组件

分享一些传值的组件

作者: youmu178 | 来源:发表于2016-07-07 17:01 被阅读563次

    IntentBuilder

    @IntentBuilder
    class DetailActivity extends Activity {
        
        @Extra
        String id;
    
        @Extra @Nullable
        String title;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            DetailActivityIntentBuilder.inject(getIntent(), this);
            // TODO use id and title
        }
    
    startActivity(new DetailActivityIntentBuilder("12345")
        .title("MyTitle")
        .build(context))
    }
    
    @IntentBuilder
    class DownloadService extends IntentService {
    
        @Extra
        String downloadUrl;
        
        @Override
        protected void onHandleIntent(Intent intent) {
            MyServiceIntentBuilder.inject(intent, this);
        }
    
    }
    
    startService(new DownloadServiceIntentBuilder("http://google.com").build(context))
    

    OnActivityResult

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        ActivityResult.onResult(requestCode, resultCode, data).into(this);
    }
    
    @OnActivityResult(requestCode = 33)
    void onActivityResultTestActivity() { /* Do something */ }
    
    @OnActivityResult(requestCode = 1, resultCodes = { Activity.RESULT_OK })
    void onActivityResultActivityOk() { /* Only do something when ok */ }
    
    @OnActivityResult(requestCode = 1, resultCodes = { Activity.RESULT_CANCELED })
    void onActivityResultActivityCanceled() { /* Only do something when canceled */ }
    
    @OnActivityResult(requestCode = 2)
    void onActivityResultPickImage(final int resultCode, final Intent intent) { /* Do something */ }
    

    支持各种参数

    • none
    • int
    • Intent
    • int, Intent
    • Intent, int

    每个注解的方法只能有一个int和/或意图变量

    FragmentArgs

    @FragmentWithArgs
    public class MyOptionalFragment extends Fragment {
    
        @Arg
        int id;
        
        @Arg
        String title;
        
        @Arg(required = false) 
        String additionalText;
        
        @Arg(required = false)
        float factor;
        
        @Arg(required = false)
        int mFeatureId;
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            FragmentArgs.inject(this); // read @Arg fields
        }
        
    }
    
    public class MyActivity extends Activity {
    
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            
            int id = 123;
            String title = "test";
            
            // Using the generated Builder
            Fragment fragment = 
                new MyFragmentBuilder(id, title) // required args
                .additionalText("foo")  // Optional arg
                .factor(1.2f)           // Optional arg
                .featureId(42)          // Optional arg
                .build();
            
            
            // Fragment Transaction
            getFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
        }
    
    }
    
    Paste_Image.png

    相关文章

      网友评论

          本文标题:分享一些传值的组件

          本文链接:https://www.haomeiwen.com/subject/ewjljttx.html