Having a refresh button - android

Lets suppose I am on screen2 on my application, and my application retrieves data from a database. I want to have a refresh button on it. So I am writing this code:
b3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Screen2.this,Screen2.class);
startActivity(i);
}
});
where b3 is the button.My question is kind of theoritically: Is there any better way? Does this make my application heavy or causes any other problems to OS if I do for example several time refresh.
Would it be better to do the refresh for example at specific times? If yes, how I will write this code?
Thanks.

you could use adapter.notifyDataSetChanged(). much more efficient
http://developer.android.com/reference/android/widget/ArrayAdapter.html#notifyDataSetChanged()
Edit: (thats only valid if you are using an ArrayAdapter)

You should not restart the activity each time you want updated information: just update your views when the update button is clicked.

Related

How to prevent an activity from being initialized multiple times

I have an Android app, when a user taps a button multiple times quickly, same activity is initialized multiple times.
To prevent this, I added android:launchMode="singleInstance" in Manifest file. But now, when an activity calls itself, it doesn't work.
I also tried
Intent myintent = getIntent();
myintent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
But this didn't work either.
How can I prevent having multiple activities when user clicks a button multiple times quickly, and how can I have same activity call itself correctly. Thanks.
Yeah, this happens if you are "trigger happy". You can also in many situations use multi-touch to activate a bunch of options at the same time. If you really need to solve this, you can look at disabling elements like J Whitfield suggested (element.setEnabled(false) or element.setClickable(false)) or intercepting onTouch.
You could try disabling the button after the first click has been detected.
Button button = theView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.isEnabled()){
v.setEnabled(false);
}
//Call your new activity here
...activity stuff...
}
});

Too many Activities in Android App. Any alternatives?

I am creating my first app. In this app, I have an expandable list with many items. When I select any of these items, I want several paragraphs to be displayed. Do I need to create an Activity for each of these items if text is the only thing I want displayed? I know that there has to be an easier way. I did create it like this at first and it seemed very bulky (30+ activities), so now I have it set up so that when an item is selected, the setContentView opens the corresponding layout with the text that needs to be displayed. This works but there is a catch, whenever I hit the back button, it takes me back to my main activity class and not my expandable list class. I want the user to be able to go back and select something else from the list. Any guidance as to what I need to do would be appreciated.
I would suggest creating string resources for each item you would like to display, then creating one activity with a TextView. Then, instead of creating new intents for each activity, create an intent that goes to the new activity, and add an extra that contains the text for the TextView. For example:
Activity1:
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(this, ParagraphView.class);
intent.putExtra("textData", getResources().getString(R.string.myText));
getBaseContext().startActivity(intent);
}
});
In the onCreate of the viewer, add this to get your TextView:
Intent intent = getIntent();
String textData = intent.getStringExtra("text");
Now, we need to write the text into the TextView:
TextView tv = (TextView) findViewById(R.id.myTextView);
tv.setText(textData);
All you have to to is set up your string resources and button click listeners. You may consider this easier than having lots of activities (it's definitely easier to manage entries this way) but does require a bit of setup.
Edit: Thanks to #ianhanniballake for pointing out a much better way (I don't even know what I was thinking at the time...)
Edit2: Wow, I REALLY messed up my code. (Hopefully) Fixed now

Double Activity window when using intent for achartengine graph

Development Environment: Eclipse 3.7.0
Developing: Android 3.2 application for Market Place
Using: aChartEngine 0.7.0
I'm new to the development scene but have done a bit of coding in the past various languages, I've created the ZopaStats(on Marketplace) app, but I'm now trying to convert a text based stats page to be displayed in a bar graph using achartengine.
I can get the graph to display from an activity via another activity i.e.:
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
but this gives me an additional activity screen i.e.:
Main Screen -> 1st Activity (Original Text Stats View) -> 2nd Activity (Graph)
Therefore, with I hit back on the graph screen, I get the blank 1st activity screen.
I hope I'm making sense here.
So what I tried to do was launch the activity from the Main Screen (i.e. my Main class) e.g.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new TemperatureChart().execute(this);
startActivity(achartIntent);
}
});
But eclipse gives me the following error in the code:
The method execute(Context) in the type TemperatureChart is not applicable for the arguments new (View.OnClickListener(){}}
I've tried letting Eclipse change the method but this then causes other problems, so I think what I'm really looking for (in a round about way) is to find out what the difference is when I can try to start the activity from another Activity class rather than starting it from the main class.
I apologise for the misuse of terms etc, as I say I'm new. I've been looking at this for a few days now but the Intent and Activity documentation doesn't help me much so I just need a few pointers.
Thanks,
In your example, the this reference that you're passing to execute() is your annonymous inner subclass of OnClickListener. This is not a context object, which is what eclipse is complaining about.
Rather, you want to pass in the activity instance. Assuming the code snippet you posted lives in a class named MyExampleActivity, then you can use MyExampleActivity.this from inside the inner class to access the instance of the containing class. You should be able to pass that to TemperatureChart.execute()
There is no difference in starting an activity from the main activity or from any other activity. You just create an intent, and call startActivity on it.
For completeness, the new code is this:
N.B. My original class is called ZopaStats.class
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent achartIntent = new MarketZopaGraph().execute(ZopaStats.this);
startActivity(achartIntent);
}
});
This works great, only a single Activity windows, once again many thanks for the quick response, in record time ;)
when we use achartengine to draw graph in includes it own activity...i.e.org.achartengine.GraphicalActivity..
when we press back it shows own activity which is used to show graph..to hide these activity call finish() method on onPause() method.

Why is there no getOnClickListener in the Button class? (Android)

Why is there no getOnClickListener in the Button class? I think this is really strange considering there is a getOnFocusChangeListener function. Why make it for the FocusChangeListener and not for the ClickListener?
Added comment:
For those below that are wondering why I need this: We are developing a large application with a lot of viewgroups on the screen. I want to add some code to a button on the screen but not replace the complete OnClickListener. I want to implement a new OnClickListener that will run some code and call the old OnClickListener. But for that I need to retrieve the old one.
I don't know why there is not, but you can do what you want to do by extending the button class:
public class Button extends android.widget.Button implements OnClickListener {
public void onClick(View v) {
/* Your code here...*/
super().onClick(v);
}
}
I think it's a question to Google :D
Why do you need to get a onClickListener back? If you are so desperate, store it in a tag (Views.setTag(...));

Which is better in implementing click listener?

Which is a good practice in implementing click listener and why? Or is there a better way other than the two? Thanks.
First :
sampleButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// do something
}
});
Second : implement OnClickListener then override onClick method?
The third option is to set the listener directly in your XML layout:
android:onClick="myClickHandler"
and then implement it in your Activity:
public void myClickHandler(View v){
// do something
}
You're technically doing the 2nd thing with the 1st one. The 1st case uses whats called an anonymous class which implements OnClickListener, but since is anonymous, doesn't have a class name and isn't editable from external classes. Explicitably implementing OnClickListener is useful if you expect to use the same onClick functionality in multiple different locations, or if the click code is long
The first approach is used when you want to perform the action only for a particular case, if many click events require the same action then use the second one.

Categories

Resources