UdhayaBalaji / androidannotations

Automatically exported from code.google.com/p/androidannotations
0 stars 0 forks source link

Provide static helpers to start activities #143

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Maybe we should provide the following static methods as part of the generated 
sub activities :

public static Intent buildStartIntent(Context context, int flags) {
    Intent intent = new Intent(context, MyActivity_.class);
    intent.setFlags(flags);
        return intent;
}

public static Intent buildStartIntent(Context context) {
        return buildStartIntent(context, 0);
}

public static void start(Context context, int flags) {
    Intent intent = buildStartIntent(context, flags);
    context.startActivity(intent);
}

public static void start(Context context) {
    start(context, 0);
}

So we could start the generated activities this way :

MyActivity_.start(this);

MyActivity_.start(this, Intent.FLAG_ACTIVITY_CLEAR_TOP | 
Intent.FLAG_ACTIVITY_SINGLE_TOP);

This trick is used in some Open Source Google apps. The idea is that the code 
related to started an activity belongs to the code of this activity. It would 
also be great to avoid the "forgot to use _" problem.

Original issue reported on code.google.com by py.ricau on 8 Dec 2011 at 11:13

GoogleCodeExporter commented 8 years ago
I vote for it !

Can we add support for startActivityForResult(Intent,int) ?

Something like that :

public static void startForResult(Context context, int flags, int requestCode) {
    Intent intent = buildStartIntent(context, flags);
    context.startActivityForResult(intent, requestCode);
}

public static void startForResult(Context context, int requestCode) {
    startForResult(context, 0, requestCode);
}

With a new annotation @OnResult :

@OnResult(MY_REQUEST_CODE)
public onResult(Intent data, int resultCode) {
 ... blah blah blah ...
}

Both parameters of the onResult method are not mandatory.

Original comment by mat.boni...@gmail.com on 8 Dec 2011 at 11:47

GoogleCodeExporter commented 8 years ago
Ok, that's interesting indeed. It would work quite like onOptionsItemSelected.

One detail though : startActivityForResult is defined in Activity, not Context 
(only activities can get activity results).

So we would probably do this :

public static void startForResult(Activity activity, int flags, int 
requestCode) {
    Intent intent = buildStartIntent(activity, flags);
    activity.startActivityForResult(intent, requestCode);
}

public static void startForResult(Activity activity, int requestCode) {
    startForResult(activity, 0, requestCode);
}

Original comment by py.ricau on 8 Dec 2011 at 12:42

GoogleCodeExporter commented 8 years ago
Moved the "@OnResult" idea to issue 154.

Original comment by py.ricau on 30 Dec 2011 at 7:19

GoogleCodeExporter commented 8 years ago
This issue was closed by revision 5d117c269dbe.

Original comment by py.ricau on 30 Dec 2011 at 7:33