I have a Bottom Navigation view with a badge drawable that shows new chats.
This badges are updated upon a listener to Firebase database, where I store the notification counter. When this value change, the badge is updated. Moreover if the counter is equal to zero, the badge is set not visible.
Everything works fine except if I change some configuration using the device Settings (such as language or removing permissions). In fact, if I do that and go back to the app, the activity is re-created (sometimes without destroying it) and badge reloaded. But the setVisibility seems not working. Even if the counter is zero the badge is visible. Plus is not update anymore when the listener is triggered.
The code works, I checked with some logs if the listener is triggered and if the lines which include setVisibility are run. It just seems to have random behaviour.
If the activity is destroyed and recreated again, it works.
Any help will be appreciated!
this is how I initialize the badge
bottomNav = findViewById(R.id.bottom_navigation);
badge_chat = bottomNav.getOrCreateBadge(R.id.nav_chat);
badge_chat.setVisible(false);
this is the listener code
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
int badge_counter = dataSnapshot.getValue(int.class);
boolean visibility;
if (badge_counter == 0) {
visibility = false;
} else {
visibility = true;
}
badge_chat.setNumber(badge_counter);
badge_chat.setVisible(visibility);
}
One way I've managed to resolve this is to create/remove badge whenever it needs to be visible/hidden. In your case, something like this should work:
if (badge_counter == 0) {
getBadge(R.id.nav_chat)?.isVisible = false
removeBadge(R.id.nav_chat)
} else {
getOrCreateBadge(R.id.nav_chat).apply{
isVisible = true
number = badge_counter
}
}
note: the answer is in Kotlin.
I am trying to write a program where when I use a button a "\" shows, then if I hit that same button a "X" shows, and finally if I hit that same button a third time "(X)" shows. Right now I have this method ......
public void display20closePlayer1(String close) {
TextView playerOneTwentyClose = (TextView) findViewById(R.id.player_one_20_close);
playerOneTwentyClose.setText(String.valueOf(close));
}
that finds the button and sets the text with this....
public void twentyCloseOutPlayer1(View v) {
playerOneClose20 = playerOneClose20 + "\\";
display20closePlayer1 (playerOneClose20);
}
I am storing the string in a public object in my main activity...
String playerOneClose20 = "";
When I run my code and press the button I get a "\" to show up, if I hit it again I get "\" and so on.
Can someone please help to explain a method that would replace the already called "\", with an "X" and then replace the "X" with "(X)" Thanks for the help!
You’re close! The way I would do this is with a global int variable that denotes the stage you’re on (i.e 1 => “/“, so on and so forth)
Then in your on button click listener get the text view and set it depending on the stage number.
Remember to set and reset that int variable otherwise it will mess up your code
I have two activities : lets say A and B
A has a button which opens B using intent concept.
Through B I am saving data in database using SQLite concept.
A is the main activity which has a textView which is showing some value !
code:
Using SharedPreferences to store int value and display in MainActivity A
c=sp.getInt(Salaryflag, 0);
str=Integer.toString(c);
tv.setText(str);
Now I am using some algorithm to calculate sum() in one row in the database a store its value in an int variable
Now in onResume I am using this concept to change the value of the A textView ! :
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ItemsDataBase xyx=new ItemsDataBase(this);
xyx.open();
int lola;
lola=xyx.getSum();
xyx.close();
c=c-lola;
str=Integer.toString(c);
tv.setText(str);
}
Now what I want is that when I go back from second Acitvity B to A it should immediately show the changes.
But actually It is showing some garbage value and when I restart the app after closing then it shows the desired changes in the textView
How to remove this logical error ?
Garbage value:
After closing the app and reopening it:
You should write c=sp.getInt(Salaryflag, 0); in your on resume too as you are assigning c-lola to c.
When you are restarting the activity, line c=sp.getInt(Salaryflag, 0) is getting executed first so it displaying perfactly.
When you coming from B to A , the onResume() of A gets called so you have to initialize/assign variable c in onResume() as well.
I recently learnt about SharedPreferences and now I tried to put it in my code. I have 2 integer values called counter and counterPS. What I want to happen is that the app will save the values every 5 seconds and then when the app is completely closed and completely re opened (phone turned off and on again for example) I want the app to see if the values that were saved (if they even were) are bigger than 0, if so then set the current values to the old saved ones. However I set a number for the values inside the app and waited five seconds for it to save and when i restarted the app completely the values just came up as 0. Why is this and can someone tell me how to fix this?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
SharedPreferences saving = getSharedPreferences("ShipData", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = saving.edit();
int counter = saving.getInt("shipCounter", 0);
int counterPS = saving.getInt("shipCounterPS", 0);
if(mShip.getCounter() == 0) {
if (counter > 0) {
mShip.setCounter(counter);
mShip.setCounterPerSec(counterPS);
}
}
//Save values every 5 seconds Below
new TimerClass(5000, 1000)
{
public void OnFinish()
{
editor.putInt("ShipCounter", mShip.getCounter());
editor.putInt("ShipCounterPS", mShip.getCounterPerSec());
editor.commit();
this.start();
}
}.start();
}
How about using handler ( the android way) instead of timer class? does it suit in your case ?
For more info:
http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/
I am aware that the question was posted a while back. Just replying to it for similar future questions ... Thanks.
I am developing an android app and I want to know how many times it has been opened. Is there a way to do this?
The problem with using onCreate in an Activity is that this will increment the counter even on orientation changes. Using onCreate in an Application also has a downside in that your counter will only be incremented when after the VM has closed - so even if the app exits and reopens this will not necessarily increment.
The truth is there is no fool-proof method for handling this sort of count, however I have come up with a very good way of doing this, that is about as close to 100% accurate as possible. It requires work in both an Application class and your main Activity class, and relies on timestamps to differentiate between orientation changes and actual app launches. To start, add the following Application class:
/**
* Application class used for correctly counting the number of times an app has been opened.
* #author Phil Brown
* #see Stack Overflow
*
*/
public class CounterApplication extends Application
{
private long lastConfigChange;
/** #param buffer the number of milliseconds required after an orientation change to still be considered the same event*/
public boolean wasLastConfigChangeRecent(int buffer)
{
return (new Date().getTime() - lastConfigChange <= buffer);
}
#Override
public void onCreate()
{
lastConfigChange = new Date().getTime();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
lastConfigChange = new Date().getTime();
}
}
You should add this Application to your AndroidManifest.xml by specifying the name application attribute:
android:name="path.to.CounterApplication"
Now, in your main Activity, add the following in onCreate:
//note that you can use getPreferences(MODE_PRIVATE), but this is easier to use from Fragments.
SharedPreferences prefs = getSharedPreferences(getPackageName(), MODE_PRIVATE);
int appOpenedCount = prefs.getInt("app_opened_count", 1);
if (!((CounterApplication) getApplication()).wasLastConfigChangeRecent(10000))//within 10 seconds - a huge buffer
{
appOpenedCount += 1;
prefs.edit().putInt("app_opened_count", appOpenedCount).commit();
}
//now say you want to do something every 10th time they open the app:
boolean shouldDoThing = (appOpenedCount % 10 == 0);
if (shouldDoThing)
{
doThing();
appOpenedCount += 1;
//this ensures that the thing does not happen again on an orientation change.
prefs.edit().putInt("app_opened_count", appOpenedCount).commit();
}
Just, declare:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
private int totalCount;
Initialize in onCreate(...) :
prefs = getPreferences(Context.MODE_PRIVATE);
editor = prefs.edit();
Print or count wherever you want (any where in onCreate or any specific click as you specified)
totalCount = prefs.getInt("counter", 0);
totalCount++;
editor.putInt("counter", totalCount);
editor.commit();
Now print totalcount where you want to count eg.:
System.out.println("Total Application counter Reach to :"+totalCount);
In your Application or Activity's onCreate() method, increment a counter stored in persistent storage such as SharedPreferences.
You can use shared preferences. Every time the app is opened, retrieve the preferences, increment the count, then store it right away. The only issue is that if a user deletes the app along with all preferences, then the count will be erased too. Here is an example of committing to the preferences. Use getPreferences to retrieve them at the startup of the app.
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("pref 1", "some text");
editor.commit();
1. For a simple approach, keep a text file where you increment the value by 1, after reading it. Keep the count increment on OnCreate() method of Activity
2. You can use SharedPreference.
3. Well DataBase can also be used...but i think that too over-kill for this....
One way:
Keep a property in preferences and on launching activity update preference count by '1', but you may not be able to see this increased value because it stays on phone.
Otherway
Call a service to your server (if you have any) to increment the visit count.
Using SharedPreference or the Database.
during OnCreate add 1 to the numberofTimes counter and commit.
OnCreate (Bundle bundle){
mPref = getPreferences();
int c = mPref.getInt("numRun",0);
c++;
mPref.edit().putInt("numRun",c).commit();
//do other stuff...
}
OnCreate is called regardless of you start the app or you resume the app, but isFinishing() returns true if and only iff the user (or you) called finish() on the app (and it was not being destroyed by the manager)
This way you only increment when you are doing fresh start.
the onFinishing() Method inside of a OnPause method to check to see if the activity is being finish() or just being paused.
#Override
protected void OnPause(){
if(!onFinishing()){
c = mPref.getInt("numRun",0);
c--;
mPref.edit().putInt("numRun",c).commit();
}
//Other pause stuff.
}
As I have said in another answer, I think the following is the best solution:
private static boolean valueOfLaunchCountModified = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
if(!valueOfCountModified){
preferences = getPreferences(MODE_PRIVATE);
launchCount= preferences.getInt("launchCount", 0);
if(preferences.edit().putInt("launchCount", ++launchCount).commit()){
valueOfCountModified = true;
}
}
if(launchCount == 5 && valueOfCountModified){
//Do whatever you want
}
}
If we remember the definition of a static variable, we will discover that is perfect for us:
They are associated with the class, rather than with any object. Every instance of the class shares a class variable.
When onPause method or an orientation change is executed the value of valueOfLaunchCountModified doesn't change; however, if the app process is destroyed, the value of valueOfLaunchCountModified changes to false.