Code Monkey home page Code Monkey logo

Comments (2)

sockeqwe avatar sockeqwe commented on July 18, 2024

From my point of view the main question is: Do you see your library as Activity navigation helper then ActivityScreens and @ActivityArg is fine. However, I could think of a more general use case of your library: IntentBuilder

What about making this library a "universal" one i.e. by renaming it to IntentBuilder with @IntentArg (or just @Arg) as annotation. So instead of focusing on Activities only, I could think of such an API:

  1. Build an Intent to start an Activity:

    public SampleActivity extends Activity {
       @IntentArg
       int id;
    
       @IntentArg
        String foo;
    
       public void onCreate(Bundle b){
            super.onCreate(b);
            IntentBuilder.inject(this, getIntent());
       }
    }
    public OtherActivity extends Activity {
    
       public void startSampleActivity(){
    
            SampleIntentBuilder
                               .from(this)
                               .to(SampleActivity.class)
                              . id(123)
                              .foo("Foo")
                              .start();
       }
    }

The SampleIntentBuilder could provide the method start() and build() and startForResult():

  • start(): starts the Intent to launch new Activity
  • startForResult(int requestCode)
  • build(): returns the Intent in case you want to start the Intent manually or want to add things like SharedElementTransitions etc.
  1. Starting Service:

    public class FooService extends Service {
    
       @IntentArg
        int id;
    
       @IntentArg
        String foo;
    
       @Override
       public int onStartCommand(Intent intent, int flags, int startId) {
          IntentBuilder.inject(this, intent);
       }
    }
    public OtherActivity extends Activity {
    
    public void startFooService(){
    
          FooServiceIntentBuilder
                             .from(this)
                             .to(FooService.class)
                            . id(123)
                            .foo("Foo")
                            .start();
     }
    }
  2. Starting IntentService:

    public class MyIntentService extends IntentService {
    
     @IntentArg
      int id;
    
    @IntentArg
    String foo;
    
    @Override
    protected void onHandleIntent(Intent intent) {
         IntentBuilder.inject(this, intent);
      }
    }
    public OtherActivity extends Activity {
    
        public void startMyIntentService(){
    
             MyIntentServiceIntentBuilder
                               .from(this)
                               .to(MyIntentService.class)
                              . id(123)
                              .foo("Foo")
                              .start();
       }
    }
  3. BroadCastReceiver:

    public class MyBroadCastReceiver extends BroadcastReceiver {
    
    public static final String ACTION ="com.example.action";
    
     @IntentArg
      int id;
    
    @IntentArg
    String foo;
    
     @Override
       public void onReceive(Context context, Intent intent) {
            IntentBuilder.inject(this, intent);
        }
    }
    public OtherActivity extends Activity {
    
     public void broadCast(){
    
     MyBroadCastReceiverIntentBuilder
                            .withAction(MyBroadCastReceiver.ACTION)
                            . id(123)
                            .foo("Foo")
                            .start();
     }
    }

I'm not sure if that really makes sense for BroadCastReceivers or IntentServices, but for that ones we could (in a second step) also generate a POJO that already fills the data and return that one from IntentBuilder.inject() something like this:

public class MyIntentService extends IntentService {

     @IntentArg
     int id;

     @IntentArg
     String foo;

     @Override
     protected void onHandleIntent(Intent intent) {
           MyIntentServicePojo pojo =  IntentBuilder.inject(this, intent);
          Log.d("Test", pojo.foo+ " " + pojo.id);
     }
 }
 class MyIntentServicePojo {
     public int id;
     public String foo;
 }

As said before, I'm not sure whether this makes sense for BroadCastReceiver or not and how much time and effort you want to put into this project. If you need help, I can help you implement some things (in august)

from activityscreens.

kboyarshinov avatar kboyarshinov commented on July 18, 2024

I like the idea of IntentBuilder for both Activities and Services. Also Builder pattern solves processing order problem #4. Thank you for suggestion!

from activityscreens.

Related Issues (6)

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.