Skip to content

Commit bd1acff

Browse files
committed
Add withAnimations to Activity intent builder
The caller should chain this method call if an animation is needed when the Activity enters/leaves. The method will call Activity.overridePendingTransitions().
1 parent 966520b commit bd1acff

4 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/builder/ActivityIntentBuilder.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ public ActivityIntentBuilder(Context context, Intent intent) {
6262
}
6363

6464
@Override
65-
public final void start() {
65+
public final PostActivityStarter start() {
6666
startForResult(-1);
67+
return new PostActivityStarter(context);
6768
}
6869

6970
@Override
70-
public abstract void startForResult(int requestCode);
71+
public abstract PostActivityStarter startForResult(int requestCode);
7172

7273
/**
7374
* Adds additional options {@link Bundle} to the start method.

‎AndroidAnnotations/androidannotations-core/androidannotations-api/src/main/java/org/androidannotations/api/builder/ActivityStarter.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ public interface ActivityStarter {
2828
* objects. It also passes the given extras, the options
2929
* {@link android.os.Bundle Bundle}, if new methods are available which
3030
* accept that.
31+
*
32+
* @return a {@link PostActivityStarter} object to optionally chain
33+
* additional actions.
3134
*/
32-
void start();
35+
PostActivityStarter start();
3336

3437
/**
3538
* Starts the {@link android.app.Activity Activity} for result, by calling
@@ -43,6 +46,9 @@ public interface ActivityStarter {
4346
* @param requestCode
4447
* this code will be returned in onActivityResult() when the
4548
* activity exits.
49+
*
50+
* @return a {@link PostActivityStarter} object to optionally chain
51+
* additional actions.
4652
*/
47-
void startForResult(int requestCode);
53+
PostActivityStarter startForResult(int requestCode);
4854
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (C) 2010-2015 eBusiness Information, Excilys Group
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed To in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package org.androidannotations.api.builder;
17+
18+
import android.app.Activity;
19+
import android.content.Context;
20+
21+
/**
22+
* Provides additional actions to be added to a start of an {@link Activity}.
23+
*/
24+
public final class PostActivityStarter {
25+
26+
private Context context;
27+
28+
/**
29+
* Creates a new instance.
30+
*
31+
* @param context
32+
* the current {@link Context} which is used to start the
33+
* {@link Activity}
34+
*/
35+
public PostActivityStarter(Context context) {
36+
this.context = context;
37+
}
38+
39+
/**
40+
* Call this to specify an explicit transition animation to perform next.
41+
*
42+
* The implementation of this method simply calls
43+
* {@link Activity#overridePendingTransition}, if the current context is an
44+
* {@link Activity}.
45+
*
46+
* @param enterAnim
47+
* A resource ID of the animation resource to use for the
48+
* incoming activity. Use 0 for no animation.
49+
* @param exitAnim
50+
* A resource ID of the animation resource to use for the
51+
* outgoing activity. Use 0 for no animation.
52+
*/
53+
void withAnimation(int enterAnim, int exitAnim) {
54+
if (context instanceof Activity) {
55+
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
56+
}
57+
}
58+
}

‎AndroidAnnotations/androidannotations-core/androidannotations/src/main/java/org/androidannotations/internal/core/helper/ActivityIntentBuilder.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.lang.model.element.VariableElement;
3030
import javax.lang.model.util.ElementFilter;
3131

32+
import org.androidannotations.api.builder.PostActivityStarter;
3233
import org.androidannotations.helper.AndroidManifest;
3334
import org.androidannotations.helper.CanonicalNameConstants;
3435
import org.androidannotations.holder.HasIntentBuilder;
@@ -116,7 +117,9 @@ private JFieldVar addFragmentConstructor(JClass fragmentClass, String fieldName)
116117
}
117118

118119
private void overrideStartForResultMethod() {
119-
JMethod method = holder.getIntentBuilderClass().method(PUBLIC, environment.getCodeModel().VOID, "startForResult");
120+
JClass postActivityStarterClass = environment.getJClass(PostActivityStarter.class);
121+
122+
JMethod method = holder.getIntentBuilderClass().method(PUBLIC, postActivityStarterClass, "startForResult");
120123
method.annotate(Override.class);
121124
JVar requestCode = method.param(environment.getCodeModel().INT, "requestCode");
122125
JBlock body = method.body();
@@ -188,6 +191,8 @@ private void overrideStartForResultMethod() {
188191
} else {
189192
activityCondition._else().invoke(contextField, "startActivity").arg(intentField);
190193
}
194+
195+
body._return(_new(postActivityStarterClass).arg(contextField));
191196
}
192197

193198
private JBlock createCallWithIfGuard(JVar requestCode, JBlock thenBlock, JExpression invocationTarget) {

0 commit comments

Comments
 (0)