androidx.lifecycle.Lifecycle.Event Java Examples

The following examples show how to use androidx.lifecycle.Lifecycle.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onStateChanged(LifecycleOwner lifecycleOwner, Event event) {
    if (mMuted) {
        return;
    }
    assertThat(event, is(mExpectedEvent));
    assertThat(mActivity.mLifecycleCallFinished, is(true));
    mEventReceived = true;
}
 
Example #2
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout layout = new FrameLayout(this);
    layout.setId(R.id.fragment_container);
    setContentView(layout);
    mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_CREATE));
    getLifecycle().addObserver(mTestObserver);
}
 
Example #3
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public void testSynchronousCall(Event event, ActivityCall preInit, ActivityCall call)
        throws Throwable {
    uiThreadTestRule.runOnUiThread(() -> {
        Intent intent = new Intent();
        ComponentName cn = new ComponentName(LifecycleTestActivity.class.getPackage().getName(),
                LifecycleTestActivity.class.getName());
        intent.setComponent(cn);
        Instrumentation instrumentation = getInstrumentation();
        try {
            Application app =
                    (Application) instrumentation.getTargetContext().getApplicationContext();
            LifecycleTestActivity testActivity =
                    (LifecycleTestActivity) instrumentation.newActivity(
                            LifecycleTestActivity.class, instrumentation.getTargetContext(),
                            null, app, intent, new ActivityInfo(), "bla", null, null, null);
            preInit.call(testActivity);
            TestObserver testObserver = new TestObserver(testActivity, event);
            testActivity.getLifecycle().addObserver(testObserver);
            testObserver.unmute();
            call.call(testActivity);

            assertThat(testObserver.mEventReceived, is(true));
        } catch (Exception e) {
            throw new Error(e);
        }
    });
}
 
Example #4
Source File: ServiceLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void awaitAndAssertEvents(Event... events) throws InterruptedException {
    //noinspection SynchronizeOnNonFinalField
    synchronized (mLoggerEvents) {
        int retryCount = 0;
        while (mLoggerEvents.size() < events.length && retryCount++ < RETRY_NUMBER) {
            mLoggerEvents.wait(TIMEOUT);
        }
        assertThat(mLoggerEvents, is(Arrays.asList(events)));
    }
}
 
Example #5
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnStopCall() throws Throwable {
    testSynchronousCall(Lifecycle.Event.ON_STOP,
            activity -> {
                getInstrumentation().callActivityOnCreate(activity, null);
                performStart(activity);
            },
            SynchronousActivityLifecycleTest::performStop);
}
 
Example #6
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnResumeCall() throws Throwable {
    testSynchronousCall(Lifecycle.Event.ON_RESUME,
            activity -> {
                getInstrumentation().callActivityOnCreate(activity, null);
                performStart(activity);
            },
            SynchronousActivityLifecycleTest::performResume);
}
 
Example #7
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPause() {
    super.onPause();
    mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_PAUSE));
    // helps with less flaky API 16 tests.
    overridePendingTransition(0, 0);
}
 
Example #8
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnStartCall() throws Throwable {
    testSynchronousCall(Lifecycle.Event.ON_START,
            activity -> getInstrumentation().callActivityOnCreate(activity, null),
            SynchronousActivityLifecycleTest::performStart);
}
 
Example #9
Source File: ServiceLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    synchronized (mLoggerEvents) {
        mLoggerEvents.add((Event) intent.getSerializableExtra(TestService.EXTRA_KEY_EVENT));
        mLoggerEvents.notifyAll();
    }
}
 
Example #10
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnCreateCall() throws Throwable {
    testSynchronousCall(Event.ON_CREATE,
            activity -> {
            },
            activity -> getInstrumentation().callActivityOnCreate(activity, null));
}
 
Example #11
Source File: ProcessOwnerTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Test
public void testPressHomeButton() throws Throwable {
    setupObserverOnResume();

    Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
            NavigationDialogActivity.class.getCanonicalName(), null, false);
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    instrumentation.addMonitor(monitor);

    NavigationTestActivityFirst activity = activityTestRule.getActivity();
    activity.startActivity(new Intent(activity, NavigationDialogActivity.class));
    FragmentActivity dialogActivity = (FragmentActivity) monitor.waitForActivity();
    checkProcessObserverSilent(dialogActivity);

    List<Event> events = Collections.synchronizedList(new ArrayList<>());

    LifecycleObserver collectingObserver = new LifecycleObserver() {
        @OnLifecycleEvent(Event.ON_ANY)
        public void onStateChanged(@SuppressWarnings("unused") LifecycleOwner provider,
                Event event) {
            events.add(event);
        }
    };
    addProcessObserver(collectingObserver);
    events.clear();
    assertThat(activity.moveTaskToBack(true), is(true));
    Thread.sleep(ProcessLifecycleOwner.TIMEOUT_MS * 2);
    assertThat(events.toArray(), is(new Event[]{ON_PAUSE, ON_STOP}));
    events.clear();
    Context context = InstrumentationRegistry.getContext();
    context.startActivity(new Intent(activity, NavigationDialogActivity.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    waitTillResumed(dialogActivity, activityTestRule);
    assertThat(events.toArray(), is(new Event[]{ON_START, ON_RESUME}));
    removeProcessObserver(collectingObserver);
    dialogActivity.finish();
}
 
Example #12
Source File: ActivityFullLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFullLifecycle() throws Throwable {
    CollectingLifecycleOwner owner = activityTestRule.getActivity();
    TestUtils.waitTillResumed(owner, activityTestRule);
    activityTestRule.finishActivity();

    TestUtils.waitTillDestroyed(owner, activityTestRule);
    List<Pair<TestEvent, Event>> results = owner.copyCollectedEvents();
    assertThat(results, is(flatMap(CREATE, START, RESUME, PAUSE, STOP, DESTROY)));
}
 
Example #13
Source File: SimpleAppFullLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFullLifecycle() throws InterruptedException {
    State currentState = ProcessLifecycleOwner.get().getLifecycle().getCurrentState();
    assertThat(currentState, is(CREATED));
    activityTestRule.launchActivity(null);
    List<Pair<TestEventType, Event>> events = SimpleAppLifecycleTestActivity.awaitForEvents();
    assertThat("Failed to await for events", events, notNullValue());
    //noinspection ConstantConditions
    assertThat(events.subList(0, 6).toArray(), is(EXPECTED_EVENTS_CONSTRUCTION));

    // TODO: bug 35122523
    for (Pair<TestEventType, Event> event: events.subList(6, 11)) {
        assertThat(event, isIn(EXPECTED_EVENTS_DESTRUCTION));
    }
}
 
Example #14
Source File: ServiceLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private EventLogger(List<Event> loggerEvents) {
    mLoggerEvents = loggerEvents;
}
 
Example #15
Source File: TooManyArgs1.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@OnLifecycleEvent(ON_ANY)
public void onAny(LifecycleOwner provider, Event event, int x) {
}
 
Example #16
Source File: ReflectiveGenericLifecycleObserver.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public void onStateChanged(LifecycleOwner source, Event event) {
    // 调用 ClassesInfoCache.CallbackInfo.invokeCallbacks()
    mInfo.invokeCallbacks(source, event, mWrapped);
}
 
Example #17
Source File: InvalidFirstArg2.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@OnLifecycleEvent(ON_ANY)
public void onStop(Event e2, Event event) {
}
 
Example #18
Source File: OnAnyMethod.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@OnLifecycleEvent(ON_ANY)
void any(LifecycleOwner provider, Event event){}
 
Example #19
Source File: TooManyArgs2.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@OnLifecycleEvent(ON_STOP)
public void onStop(LifecycleOwner provider, Event event) {
}
 
Example #20
Source File: InvalidFirstArg1.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@OnLifecycleEvent(ON_STOP)
public void onStop(Event event) {
}
 
Example #21
Source File: UpdateManager.java    From InAppUpdater with MIT License 4 votes vote down vote up
@OnLifecycleEvent(Event.ON_RESUME)
private void onResume() {
    continueUpdate();
}
 
Example #22
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private TestObserver(LifecycleTestActivity activity, Event expectedEvent) {
    this.mActivity = activity;
    this.mExpectedEvent = expectedEvent;
}
 
Example #23
Source File: SynchronousActivityLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Test
public void testOnDestroyCall() throws Throwable {
    testSynchronousCall(Lifecycle.Event.ON_DESTROY,
            activity -> getInstrumentation().callActivityOnCreate(activity, null),
            activity -> getInstrumentation().callActivityOnDestroy(activity));
}
 
Example #24
Source File: ProcessOwnerTest.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@OnLifecycleEvent(Event.ON_ANY)
void onEvent() {
    mChangedState = true;
}
 
Example #25
Source File: TestObserver.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
TestObserver(List<Pair<TestEvent, Event>> collectedEvents) {
    mCollectedEvents = collectedEvents;
}
 
Example #26
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public List<Pair<TestEvent, Event>> copyCollectedEvents() {
    return new ArrayList<>(mCollectedEvents);
}
 
Example #27
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onStop() {
    super.onStop();
    mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_STOP));
}
 
Example #28
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDestroy() {
    super.onDestroy();
    mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_DESTROY));
}
 
Example #29
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_RESUME));
}
 
Example #30
Source File: CollectingSupportActivity.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onStart() {
    super.onStart();
    mCollectedEvents.add(new Pair<>(OWNER_CALLBACK, Event.ON_START));
}