androidx.lifecycle.LifecycleOwner Java Examples

The following examples show how to use androidx.lifecycle.LifecycleOwner. 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: PowerMenuUtils.java    From PowerMenu with Apache License 2.0 6 votes vote down vote up
public static CustomPowerMenu getCustomDialogPowerMenu(
    Context context, LifecycleOwner lifecycleOwner) {
  return new CustomPowerMenu.Builder<>(context, new CustomDialogMenuAdapter())
      .setHeaderView(R.layout.layout_custom_dialog_header)
      .setFooterView(R.layout.layout_custom_dialog_footer)
      .addItem(
          new NameCardMenuItem(
              ContextCompat.getDrawable(context, R.drawable.face3),
              "Sophie",
              context.getString(R.string.board3)))
      .setLifecycleOwner(lifecycleOwner)
      .setAnimation(MenuAnimation.SHOW_UP_CENTER)
      .setWidth(800)
      .setMenuRadius(10f)
      .setMenuShadow(10f)
      .build();
}
 
Example #2
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testLifecycle(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get());

    TestObserver<Lifecycle.Event> testObserver = provider.lifecycle().test();

    controller.create();
    controller.start();
    controller.resume();
    controller.pause();
    controller.stop();
    controller.destroy();

    testObserver.assertValues(
            Lifecycle.Event.ON_CREATE,
            Lifecycle.Event.ON_START,
            Lifecycle.Event.ON_RESUME,
            Lifecycle.Event.ON_PAUSE,
            Lifecycle.Event.ON_STOP,
            Lifecycle.Event.ON_DESTROY
    );
}
 
Example #3
Source File: DerivedFromJar_LifecycleAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
        MethodCallsLogger logger) {
  boolean hasLogger = logger != null;
  if (onAny) {
    return;
  }
  if (event == Lifecycle.Event.ON_START) {
    if (!hasLogger || logger.approveCall("doOnStart", 1)) {
      mReceiver.doOnStart();
    }
    if (!hasLogger || logger.approveCall("doAnother", 1)) {
      mReceiver.doAnother();
    }
    return;
  }
  if (event == Lifecycle.Event.ON_PAUSE) {
    if (!hasLogger || logger.approveCall("doOnPause", 2)) {
      LibraryBaseObserver_LifecycleAdapter.__synthetic_doOnPause(mReceiver,owner);
    }
    return;
  }
}
 
Example #4
Source File: ManageNetKeyAdapter.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public ManageNetKeyAdapter(@NonNull final LifecycleOwner owner,
                           @NonNull final LiveData<ProvisionedMeshNode> meshNodeLiveData,
                           @NonNull final List<NetworkKey> netKeys) {
    meshNodeLiveData.observe(owner, node -> {
        networkKeys.clear();
        for (NodeKey key : node.getAddedNetKeys()) {
            for (NetworkKey networkKey : netKeys) {
                if (networkKey.getKeyIndex() == key.getIndex()) {
                    networkKeys.add(networkKey);
                }
            }
        }
        Collections.sort(networkKeys, Utils.netKeyComparator);
        notifyDataSetChanged();
    });
}
 
Example #5
Source File: InterfaceOk2Derived_LifecycleAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
    MethodCallsLogger logger) {
  boolean hasLogger = logger != null;
  if (onAny) {
    return;
  }
  if (event == Lifecycle.Event.ON_STOP) {
    if (!hasLogger || logger.approveCall("onStop1", 2)) {
      mReceiver.onStop1(owner);
    }
    if (!hasLogger || logger.approveCall("onStop2", 2)) {
      mReceiver.onStop2(owner);
    }
    if (!hasLogger || logger.approveCall("onStop3", 2)) {
      mReceiver.onStop3(owner);
    }
    return;
  }
}
 
Example #6
Source File: AndroidLifecycleActivityTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testBindUntilEvent(ActivityController<? extends LifecycleOwner> controller) {
    LifecycleProvider<Lifecycle.Event> activity = AndroidLifecycle.createLifecycleProvider(controller.get());

    TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test();

    controller.create();
    testObserver.assertNotComplete();
    controller.start();
    testObserver.assertNotComplete();
    controller.resume();
    testObserver.assertNotComplete();
    controller.pause();
    testObserver.assertNotComplete();
    controller.stop();
    testObserver.assertComplete();
}
 
Example #7
Source File: DifferentPackagesDerived1_LifecycleAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
    MethodCallsLogger logger) {
  boolean hasLogger = logger != null;
  if (onAny) {
    return;
  }
  if (event == Lifecycle.Event.ON_STOP) {
    if (!hasLogger || logger.approveCall("onStop", 2)) {
      DifferentPackagesBase1_LifecycleAdapter.__synthetic_onStop(mReceiver,owner);
    }
    if (!hasLogger || logger.approveCall("onStop2", 2)) {
      mReceiver.onStop2(owner);
    }
    return;
  }
}
 
Example #8
Source File: LiveDataFragment.java    From tv-samples with Apache License 2.0 6 votes vote down vote up
private void subscribeNetworkInfo() {
    NetworkLiveData.sync(getActivity())
            .observe((LifecycleOwner) getActivity(), new Observer<Boolean>() {
                @Override
                public void onChanged(@Nullable Boolean aBoolean) {
                    if (aBoolean) {
                        getActivity().findViewById(R.id.no_internet).setVisibility(View.GONE);

                        // TODO: an appropriate method to re-create the database
                    } else {
                        getActivity().findViewById(R.id.no_internet)
                                .setVisibility(View.VISIBLE);
                    }
                }
            });
}
 
Example #9
Source File: AndroidLifecycleFragmentTest.java    From RxLifecycle with Apache License 2.0 6 votes vote down vote up
private void testLifecycle(LifecycleOwner owner) {
    Fragment fragment = (Fragment) owner;
    ActivityController<?> controller = startFragment(fragment);

    TestObserver<Lifecycle.Event> testObserver = AndroidLifecycle.createLifecycleProvider(owner).lifecycle().test();

    controller.start();
    controller.resume();
    controller.pause();
    controller.stop();
    controller.destroy();

    testObserver.assertValues(
            Lifecycle.Event.ON_CREATE,
            Lifecycle.Event.ON_START,
            Lifecycle.Event.ON_RESUME,
            Lifecycle.Event.ON_PAUSE,
            Lifecycle.Event.ON_STOP,
            Lifecycle.Event.ON_DESTROY
    );
}
 
Example #10
Source File: VideoCardPresenter.java    From tv-samples with Apache License 2.0 6 votes vote down vote up
CardViewHolder(ImageCardView view, Context context) {
    super(view);
    mContext = context;
    Context wrapper = new ContextThemeWrapper(mContext, R.style.MyPopupMenu);
    mPopupMenu = new PopupMenu(wrapper, view);
    mPopupMenu.inflate(R.menu.popup_menu);

    mPopupMenu.setOnMenuItemClickListener(this);
    view.setOnLongClickListener(this);

    mOwner = (LifecycleOwner) mContext;

    mDefaultBackground = mContext.getResources().getDrawable(R.drawable.no_cache_no_internet, null);
    mDefaultPlaceHolder = new RequestOptions().
            placeholder(mDefaultBackground);

    mCardView = (ImageCardView) CardViewHolder.this.view;
    Resources resources = mCardView.getContext().getResources();
    mCardView.setMainImageDimensions(Math.round(
            resources.getDimensionPixelSize(R.dimen.card_width)),
            resources.getDimensionPixelSize(R.dimen.card_height));

    mFragmentActivity = (FragmentActivity) context;
    mViewModel = ViewModelProviders.of(mFragmentActivity).get(VideosViewModel.class);
}
 
Example #11
Source File: DifferentPackagesDerived2_LifecycleAdapter.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
    MethodCallsLogger logger) {
  boolean hasLogger = logger != null;
  if (onAny) {
    return;
  }
  if (event == Lifecycle.Event.ON_STOP) {
    if (!hasLogger || logger.approveCall("onStop", 2)) {
      mReceiver.onStop(owner);
    }
    if (!hasLogger || logger.approveCall("onStop2", 2)) {
      mReceiver.onStop2(owner);
    }
    return;
  }
}
 
Example #12
Source File: AdapterRuleMatch.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
AdapterRuleMatch(Context context, LifecycleOwner owner) {
    this.context = context;
    this.owner = owner;
    this.inflater = LayoutInflater.from(context);

    this.D = new SimpleDateFormat("E");
    this.DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);

    setHasStableIds(true);

    owner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        public void onDestroyed() {
            Log.d(AdapterRuleMatch.this + " parent destroyed");
        }
    });
}
 
Example #13
Source File: NoPackageTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    mLifecycleOwner = mock(LifecycleOwner.class);
    mLifecycle = mock(Lifecycle.class);
    when(mLifecycleOwner.getLifecycle()).thenReturn(mLifecycle);
    mRegistry = new LifecycleRegistry(mLifecycleOwner);
}
 
Example #14
Source File: SimpleDialog.java    From Snake with Apache License 2.0 5 votes vote down vote up
private void init(Context context) {
    dataBinding = SimpleDialogBinding.inflate(LayoutInflater.from(getContext()));

    if (context instanceof LifecycleOwner) {
        dataBinding.setLifecycleOwner((LifecycleOwner) context);
    }
    setContentView(dataBinding.getRoot());
}
 
Example #15
Source File: SearchFragment.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
private void subscribeNetwork() {
    NetworkLiveData.sync(this.getActivity())
            .observe((LifecycleOwner) this.getActivity(), new Observer<Boolean>() {
                @Override
                public void onChanged(@Nullable Boolean isNetworkAvailable) {
                    if (isNetworkAvailable) {
                        getActivity().findViewById(R.id.no_internet_search)
                                .setVisibility(View.GONE);
                    } else {
                        getActivity().findViewById(R.id.no_internet_search)
                                .setVisibility(View.VISIBLE);
                    }
                }
            });
}
 
Example #16
Source File: FlapAdapter.java    From Flap with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToRecyclerView(@NonNull final RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    if (recyclerView.getContext() instanceof LifecycleOwner && lifecycleOwner == null) {
        setLifecycleOwner((LifecycleOwner) recyclerView.getContext());
    }
    if (useFlapItemPool) {
        recyclerView.setRecycledViewPool(flap.getComponentPool());
    }
}
 
Example #17
Source File: SingleLiveEvent.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@MainThread
public void observe(@NonNull final LifecycleOwner owner, @NonNull final Observer<? super T> observer) {
    if (hasActiveObservers()) {
        Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
    }

    // Observe the internal MutableLiveData
    super.observe(owner, t -> {
        if (mPending.compareAndSet(true, false)) {
            observer.onChanged(t);
        }
    });
}
 
Example #18
Source File: SingleLiveEvent.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@MainThread
public void observe(@NonNull LifecycleOwner owner, @NonNull final Observer<? super T> observer) {
  if (hasActiveObservers()) {
    Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
  }

  // Observe the internal MutableLiveData
  super.observe(owner, t -> {
    if (mPending.compareAndSet(true, false)) {
      observer.onChanged(t);
    }
  });
}
 
Example #19
Source File: MeteredConnectivityObserver.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@MainThread
MeteredConnectivityObserver(@NonNull Context context, @NonNull LifecycleOwner lifecycleOwner) {
  this.context             = context;
  this.connectivityManager = ServiceUtil.getConnectivityManager(context);
  this.metered             = new MutableLiveData<>();

  this.metered.setValue(ConnectivityManagerCompat.isActiveNetworkMetered(connectivityManager));
  lifecycleOwner.getLifecycle().addObserver(this);
}
 
Example #20
Source File: MVPFragment.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
private void onLifecycleOwnerStateChanged(@NonNull LifecycleOwner source,
                                          @NonNull Lifecycle.Event event) {
    switch (event) {
        case ON_CREATE:
            mViewHolder.setView(this);
            break;
        case ON_DESTROY:
            mViewHolder.setView(null);
            source.getLifecycle().removeObserver(mLifecycleEventObserver);
            break;
    }
}
 
Example #21
Source File: AdapterNavAccount.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
AdapterNavAccount(Context context, LifecycleOwner owner) {
    this.context = context;
    this.owner = owner;
    this.inflater = LayoutInflater.from(context);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean highlight_unread = prefs.getBoolean("highlight_unread", true);
    this.colorUnread = Helper.resolveColor(context, highlight_unread ? R.attr.colorUnreadHighlight : android.R.attr.textColorPrimary);
    this.textColorSecondary = Helper.resolveColor(context, android.R.attr.textColorSecondary);

    this.DTF = Helper.getTimeInstance(context, SimpleDateFormat.SHORT);

    setHasStableIds(true);
}
 
Example #22
Source File: BackupPackagesAdapter.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
public BackupPackagesAdapter(Selection<String> selection, LifecycleOwner lifecycleOwner, Context c) {
    super(selection, lifecycleOwner);

    mInflater = LayoutInflater.from(c);
    setHasStableIds(true);

    mFeatureViewPool = new RecyclerView.RecycledViewPool();
    mFeatureViewPool.setMaxRecycledViews(0, 16);
}
 
Example #23
Source File: TooltipUtil.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
public static void showTooltip(
        @NonNull Context context,
        @StringRes int message,
        @NonNull ArrowOrientation orientation,
        @NonNull View anchor,
        @NonNull LifecycleOwner lifecycleOwner
) {
    String prefName = "tooltip." + getViewName(anchor);
    if (context instanceof Activity) prefName += "." + ((Activity)context).getLocalClassName();

    Balloon balloon = new Balloon.Builder(context)
            .setArrowSize(10)
            .setArrowOrientation(orientation)
            .setArrowVisible(true)
            .setPadding(4)
            .setTextSize(15f)
            .setArrowPosition(0.5f)
            .setCornerRadius(4f)
            .setAlpha(0.9f)
            .setTextResource(message)
            .setTextColor(ContextCompat.getColor(context, R.color.white_opacity_87))
            .setIconDrawable(ContextCompat.getDrawable(context, R.drawable.ic_help_outline))
            .setBackgroundColor(ContextCompat.getColor(context, R.color.dark_gray))
            .setDismissWhenClicked(true)
            .setDismissWhenTouchOutside(true)
            .setBalloonAnimation(BalloonAnimation.OVERSHOOT)
            .setLifecycleOwner(lifecycleOwner)
            .setPreferenceName(prefName)
            .build();

    if (orientation.equals(ArrowOrientation.BOTTOM)) balloon.showAlignTop(anchor);
    else if (orientation.equals(ArrowOrientation.TOP)) balloon.showAlignBottom(anchor);
    else if (orientation.equals(ArrowOrientation.LEFT)) balloon.showAlignRight(anchor);
    else if (orientation.equals(ArrowOrientation.RIGHT)) balloon.showAlignLeft(anchor);
}
 
Example #24
Source File: LiveEventBusCore.java    From LiveEventBus with Apache License 2.0 5 votes vote down vote up
@MainThread
private void observeInternal(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
    ObserverWrapper<T> observerWrapper = new ObserverWrapper<>(observer);
    observerWrapper.preventNextEvent = liveData.getVersion() > ExternalLiveData.START_VERSION;
    liveData.observe(owner, observerWrapper);
    logger.log(Level.INFO, "observe observer: " + observerWrapper + "(" + observer + ")"
            + " on owner: " + owner + " with key: " + key);
}
 
Example #25
Source File: SingleLiveEvent.java    From Android-nRF-Blinky with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@MainThread
@Override
public void observe(@NonNull final LifecycleOwner owner, @NonNull final Observer<? super T> observer) {
	if (hasActiveObservers()) {
		Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
	}

	// Observe the internal MutableLiveData
	super.observe(owner, t -> {
		if (pending.compareAndSet(true, false)) {
			observer.onChanged(t);
		}
	});
}
 
Example #26
Source File: SingleLiveEvent.java    From mcumgr-android with Apache License 2.0 5 votes vote down vote up
@MainThread
public void observe(@NonNull final LifecycleOwner owner, @NonNull final Observer<? super T> observer) {

    if (hasActiveObservers()) {
        Timber.w("Multiple observers registered but only one will be notified of changes.");
    }

    // Observe the internal MutableLiveData
    super.observe(owner, t -> {
        if (mPending.compareAndSet(true, false)) {
            observer.onChanged(t);
        }
    });
}
 
Example #27
Source File: InheritanceOk3Base_LifecycleAdapter.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void callMethods(LifecycleOwner owner, Lifecycle.Event event, boolean onAny,
    MethodCallsLogger logger) {
  boolean hasLogger = logger != null;
  if (onAny) {
    return;
  }
  if (event == Lifecycle.Event.ON_STOP) {
    if (!hasLogger || logger.approveCall("onStop", 2)) {
      mReceiver.onStop(owner);
    }
    return;
  }
}
 
Example #28
Source File: ProvisionerAdapter.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ProvisionerAdapter(@NonNull final LifecycleOwner owner, @NonNull final MeshNetworkLiveData meshNetworkLiveData) {
    meshNetworkLiveData.observe(owner, networkData -> {
        final MeshNetwork network = meshNetworkLiveData.getMeshNetwork();
        final List<Provisioner> provisioners = network.getProvisioners();
        mProvisioners.clear();
        mProvisioners.addAll(provisioners);
        final Provisioner provisioner = network.getSelectedProvisioner();
        mProvisioners.remove(provisioner);
        notifyDataSetChanged();
    });
}
 
Example #29
Source File: FragmentOptionsSynchronize.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
AdapterAccountExempted(LifecycleOwner owner, Context context) {
    this.owner = owner;
    this.context = context;
    this.inflater = LayoutInflater.from(context);

    setHasStableIds(true);
}
 
Example #30
Source File: ManageAppKeyAdapter.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ManageAppKeyAdapter(@NonNull final LifecycleOwner owner, @NonNull final MeshNetworkLiveData meshNetworkLiveData) {
    meshNetworkLiveData.observe(owner, networkData -> {
        final List<ApplicationKey> keys = networkData.getAppKeys();
        if (keys != null) {
            appKeys.clear();
            appKeys.addAll(keys);
            Collections.sort(appKeys, Utils.appKeyComparator);
        }
        notifyDataSetChanged();
    });
}