Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Check if activity is active, and if active pass data from service to activity?

Posted on November 11, 2022 By

Solution 1 :

You can’t reliably determine if an Activity is active. What you should do is to store the data somewhere (file, SQLite database, etc.), and then send a broadcast Intent that means “new data is available”. Your Activity can register a listener that will get triggered by that broadcast Intent if the Activity is alive when the broadcast Intent is sent. It can then pick up the data from wherever you put it and do whatever you want with it.

Solution 2 :

There is a simple method but it doesn’t require JSON data. you can just add data as public static data.

public static int GRID_COUNT = 2;

If you are using that data for reading purposes, you can do it like this.

public static final int GRID_COUNT = 2;

Problem :

I have a service called EventReceivingSevice which will get new data in onDataRefresh(JSONObject data) function.

private void onNewData(JSONData data) {
    boolean isActive=isActivityActive(); //Check if activity is active
    if(isACtive)
         passData(data);
    else
         storeData(data);
}

An activity called HomeActivity will display the data. When EventReceivingService will get new data, it has to be checked if HomeActivity is active, it has to pass the data to HomeActivity, and if not it will store the data somewhere so that HomeActivity will later use that.

The data is in JSON format.

How can achieve this?

Comments

Comment posted by Lifecycle Aware

You can use the

Comment posted by talentica.com/blogs/android-life-cycle-aware-components

@tomerpacific Do I need to use android.arch (

Comment posted by Link

I don’t know why you went to a tutorial instead of the documentation link I sent. In the documentation itself, there is a link that shows how to add the android.lifecycle dependency.

READ  [FIXED] java - List array from room database is empty
Powered by Inline Related Posts

Comment posted by TrueCoder

@tomerpacific Can you please provide an implementation of Lifecycle Aware?

Comment posted by David Wasser

How large is this data?

Comment posted by TrueCoder

But, what about storing data? How will I get to that when I have to store data in file?

Comment posted by question

check this

Android Tags:android, android-intent, data-transfer, pass-data, service

Post navigation

Previous Post: [FIXED] Unable to connect Firebase in Android Emulator for Flutter Project
Next Post: [FIXED] android – Highlight only one Card in Compose through selection

Related Posts

[FIXED] java – Application not compatible with older Android versions Android
[FIXED] android – How to manually verify apk signatures and compare signing keys Android
[FIXED] Where to find open source code of android Espresso library Android
[FIXED] android – How to use nav arg to move to next fragment? Android
[FIXED] Use Getter Method and Return “0” in Android Android
[FIXED] android – Does not show anything in Parsing JSON for Recycler View Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme