I've implemented a library from github in my app. I want to know how to disable it after its first launch. I dont want it to be shown each time on app launch. heres what i've tried. I tried adding a boolean variable but it didnt work.
This question isn't just for this topic, suppose i want to call a method ONLY AFTER FIRST TIME after app install, I dont want it to be called again once its called in first app launch time. i hope it's clear what i'm trying to achieve.
boolean firstLoad;
if(firstLoad=true) {
TapTargetView.showFor(this, // `this` is an Activity
TapTarget.forView(findViewById(R.id.button), "This is a target", "We have the best targets, believe me")
// All options below are optional
.outerCircleColor(R.color.colorPrimary) // Specify a color for the outer circle
.outerCircleAlpha(0.96f) // Specify the alpha amount for the outer circle
.targetCircleColor(R.color.colorAccent2) // Specify a color for the target circle
.titleTextSize(20) // Specify the size (in sp) of the title text
.titleTextColor(R.color.colorAccent2) // Specify the color of the title text
new TapTargetView.Listener() { // The listener can listen for regular clicks, long clicks or cancels
#Override
public void onTargetClick(TapTargetView view) {
super.onTargetClick(view); // This call is optional
}
});
}
firstLoad=false;
Boolean firstLoad = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.getBoolean("firstLoad", true);
if (firstLoad) {
//call your method
getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("firstLoad", false).commit();
}
Related
I'm creating a media player in JavaFX. In one of my methods, I've created a way to search for metadata in a Media-file and then display it in ImageView. Works fine first time, but as soon as I want to call it again using another Media object, the image doesn't show up. I'm a bit confused and inexperienced, but I think that perhaps I need to reset/stop the listener before going to next object in line?
So my question is! How do you remove the listener when "image" has been found, what do you type to make it happen?
If you think that there's another reason why my image wont display the second time, please let me know as well.
Thanks on purpose.
private void displayAlbumCover (){
// Will start to show a blank CD
File file = new File("src/sample/images/blank_cd.jpeg");
Image image = new Image(file.toURI().toString());
albumCoverView.setImage(image);
// However if an album cover is found in the meta-data it will be displayed
ObservableMap<String,Object> meta_data=me.getMetadata();
meta_data.addListener((MapChangeListener<String, Object>) ch -> {
if(ch.wasAdded()){
String key=ch.getKey();
Object value=ch.getValueAdded();
switch(key){
case "image":
albumCoverView.setImage((Image)value);
break;
}
}
});
}
ObservableMap has removeListner method. You can keep the listener instance to variable and then remove it later.
private MapChangeListener<String, Object> listener;
private void displayAlbumCover (){
// ...
this.listener = //...
meta_data.addListener(listener);
}
private void removeListener() {
me.getMetadata().removeListener(this.listener);
}
https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableMap.html#removeListener-javafx.collections.MapChangeListener-
I'm making my custom launch configuration type. I implemented the launch configuration tab and faced the strange problem. When I do the following
private void update() {
setDirty(true);
updateLaunchConfigurationDialog();
}
in one place of my launch configuration tab class, it works fine and Apply button becomes enabled. But when I do it in another place, it doesn't work. I found something similar at https://www.eclipse.org/forums/index.php/t/164755/ , but it didn't help me to solve this problem.
See the code fragments below.
addButton.addMouseListener(new MouseListenerAdapter() {
#Override
public void mouseDown(MouseEvent e) {
moveSelectionToTableViewer(tree.getViewer().getTree().getSelection());
table.refresh();
update(); // Apply button is enabled
}
private void moveSelectionToTableViewer(TreeItem[] selection) {
// ...
}
});
removeButton.addMouseListener(new MouseListenerAdapter() {
#Override
public void mouseDown(MouseEvent e) {
int[] selectionIndices = table.getTable().getSelectionIndices();
table.getTable().remove(selectionIndices);
tree.getViewer().refresh();
update(); // Apply button is NOT enabled!
}
});
How can I solve this?
I don't know your problem from this information alone, But just a few things to check:
Have you verified that setDirty(true) is being called (e.g. with println or breakpoint?)
Have you put a watch on org.eclipse.debug.ui.AbstractLaunchConfigurationTab.fDirty to see if it changes back?
Are you overriding isDirty?
Is removing the an item from the table causing the launch configuration to become invalid in some way, i.e. you can't Apply when invalid values are in the launch config. For example, to be saveable, canSave must return true for all the tabs that are part of the launch configuration.
This is (one of the) place(s) that sets the enabled state of the Apply button:
org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationTabGroupViewer.updateButtons()
/**
* updates the button states
*/
private void updateButtons() {
boolean dirty = isDirty() && canSave();
fApplyButton.setEnabled(dirty);
fRevertButton.setEnabled(dirty);
}
Consider if a mouse listener is what you want. Note that you are responding to MouseDown, that may not do what you expect if a person tabs over to the control and presses Enter/Space instead. The more typical thing to do would be an addSelectionListener for a button. (Could it even be that responding to the event at this unusual time is causing the problem?)
I am currently developing a javafx desktop application. It contains two observableList<Item>s and two HashMap<String,Double>s. I am currently develop the menuBar , which contains these menuItem s, i.e. Open, New, Save and Save As.
Consider the case where I have started the desktop application and added a few Item to the observableList<Item>. Then all of a sudden, I want to hit any one of the menuItems listed above. First thing I want to check in my program is whether the current workflow needs to be saved before proceeding to start a new workflow (New menuitem).
I have the following method in place at the moment but I think it looks very clumsy and inefficient.
The method I developed is to set a variable private static final boolean isSaved = false;
And then within the two observableLists, I added a Listener to them:
obslist1.addListener(new ListChangeListener<Item>(){
#Override
public void onChanged(ListChangeListener.Change change) {
isSaved = false;
}
});
The code for obslist2 is identical. And the isSaved variable is set to true only if the user actually presses the Save or Save As menuItem.
I find my method very clumsy and inefficient. Is there a better way to do this?
You can do something like
BooleanProperty saved = new SimpleBooleanProperty();
InvalidationListener savedListener = obs -> saved.set(false);
and then
private void save() {
// save the data...
// mark data as saved:
saved.set(true);
}
with
obslist1.addListener(savedListener);
obslist2.addListener(savedListener);
anythingElseThatMightChangeAndIsImportant.addListener(savedListener);
Your save button and menu item, etc can do
saveButton.disableProperty().bind(saved);
Hello i have develop an application containing a list with each row a toggle button a text and an image toggle button status either 1 o 0 is stored in isChk integer list of size = number of contacts ... whenever a contact calls the call listener checks if this contact button is pressed or not from the list and perform an action
everything is running quite fine whenever i close the application the calllistener still picks up the call but does not act according to the set buttons it is reset and perform the default action rather than the needed one
how can i keep my applications array available so when the app is closed and the calllistener function picks up the call it acts accordingly?
this is the button array initialization code
public void onClick(View v) {
if(tb.isChecked()){
isChk.set(position,1);
isChkb.set(position,true);
iv.setImageResource(R.drawable.ring);
}else{
isChk.set(position,0);
isChkb.set(position,false);
iv.setImageResource(R.drawable.silentr);
}
and this is the caller function
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
// React to incoming call.
number=incomingNumber;
// If phone ringing
if(state==TelephonyManager.CALL_STATE_RINGING)
{
String CallerName = getContactDisplayNameByNumber(number);
for (String Curval : myArr){
if (Curval.contains(CallerName)){
found = true;
CallerIndex = (Integer) myArr.indexOf(Curval);
}
}
if (found){
if(isChk.get(CallerIndex)==1){
mAudioManager.setRingerMode(ringerMode1);
}
else{
mAudioManager.setRingerMode(ringerMode2);
}
}
}
if(state==TelephonyManager.CALL_STATE_IDLE)
{mAudioManager.setRingerMode(ringerMode);
}
found = false;
}
};
Storing stuff outside of your app could be what you need for this. Have a look at this
http://developer.android.com/guide/topics/data/data-storage.html#pref
Might be useful.
This is because your settings are not persistent and get lost when app is closed.
As contact list can vary, Use database to store the changes. Check in the database instead of list when call rings.
I am trying to use the MVC design.
In the model I wanted a method like this
public boolean changeSomeData(...){
boolean b;
//create a dialog with an OK button
return b;
}
I want the method to return TRUE if the changes were actually made. The changes are done inside the actionPerformed method of the OK button.
My problem is that I can't write b=true; inside the actionPerform of the OK button, because I have to declare b as final in order to use it in the actionPerformed().
What I did is creating a class
private class MyBoolean {
boolean b;
}
and then
public boolean changeSomeData(...){
MyBoolean myBoolean;
//create a dialog with an OK button
actionPerformed(){
//make changes in the data
myBoolean.b=true;
}
boolean b = myBoolean.b;
return b;
}
But I don't feel good about this solution and I wanted to know if it is correct what I did and if there is a better solution.
Should I better throw an exception if the changes aren't made? (for example, if the user clicks "cancel" instead of "ok")
In the model I wanted a method like this ... //create a dialog with an OK button
I'd say this is a flaw already, since the model should not do anything with views directly.
A better approach would be to open the dialog (using the controller), register the controller for the ActionEvent of "OK" (and thus actionPerformed) and then do whatever changes should be done in that method.
Edit:
You might want to consider the following rough approach:
The views register themselves or associated classes to the model as listeners. Whenever the model is changed it fires events to notify the views of the change.
The controller registers itself on the views and is notified when the views change. If a user changes data, the controller then might open the dialog and only commit the changes of the user signals "OK". Thus the model has never to check itself if data needs to be changed. That is actually the controller's task and if the controller passes changes to the model, it should apply them.
A better way to achieve your task is to keep a variable on the dialog that indicates if a successful change was made. Then have a method that your model class calls to retrieve the value and return it.
Something like:
public boolean changeSomeData(...){
//create a dialog with an OK button
return dialog.isSuccess();
}
One way you could make this code a bit cleaner...
public boolean changeSomeData() {
// note that this is not a class boolean, no need to do extra autoboxing.
boolean dataChanged = false;
// check the old value against the new value
// for classes
if (oldvalue.equals(newValue)) {
oldValue = newValue;
dataChanged = true;
}
// for pimitives (built-ins)
if (oldvalue == newValue) {
oldValue = newValue;
dataChanged = true;
}
// odds are good that the above action performed was supposed to call this
// changeSomeData() and not the other way around.
// if you must fire actionPerformed() when data has changed, then do so
// like this, otherwise if it was added as part of the "solution" you can
// skip it.
if (dataChanged) {
actionPeformed();
}
return dataChanged;
}
Note that this code is Controller code, as it manipulates the model directly, and (possibly) updates views.