I don't understand the meaning, value, or importance of RESULT_FIRST_USER, other than that my own result codes must be greater than 1. Will someone please explain it? RESULT_OK and RESULT_CANCELED make perfect sense to an english speaker. But what in the world of android is RESULT_FIRST_USER? All the documentations says about it is
Start of user-defined activity results.
The answer to the question is actually the combination of comments from #CommonsWare and #Chris. So, for the sake of progeny, I am going to consolidate the comments and make it available in one place.
Basically, there are two predefined constants for the requestCode and they are Activity.RESULT_OK and Activity.RESULT_CANCELLED. However, android developers can also set custom codes for their apps by using the offset Activity.RESULT_FIRST_USER. Doing so ensures that there are no clashes between constants set at the OS level and the app level.
Purely, my opinion, I think that the FIRST USER suffix is meant to refer to developers – just like how end consumer refers to consumers of a said product – who are the first users before the app users.
Below is an example of how you can use this offset,
public static final int MY_RESULT_CODE = Activity.RESULT_FIRST_USER + 1;
When an activity ends, it can call setResult(int) to return data back to its parent.
It must always supply a result code, which can be the standard results
RESULT_CANCELED (Standard activity result: Operation canceled. Constant Value: 0)
RESULT_OK (Standard activity result: operation succeeded. Constant Value: -1), or any custom values starting at RESULT_FIRST_USER (Start of user-defined activity results. Constant Value: 1). In addition, it can optionally return back an Intent containing any additional data it wants.
So, bottom line since you must supply the result code Android "helps' you a bit by saying: please state if the result code of this Activity is OK, CANCELED or you have your own, custom made, result.
When you finish an activity, you can call setResult(RESULT_CODE) to send back data to another activity. If you don't call this method, the default value will be RESULT_CANCELLED (which equals 0)
Example of returning data:
Intent intent= new Intent();
intent.putExtra("data",data);
setResult(YOUR_RESULT_CODE,intent);
finish();
An activity result is a 32-bit integer. The possible values are divided into three ranges:
-2 and below: Unused.
-1 and 0: System-defined values. That is, activity results defined by Android.
1 and above: User-defined values. That is, activity results defined by app developers.
RESULT_FIRST_USER identifies the first value in the user-defined range. The following sample definitions show how system- and user-defined values fit together:
public static final int RESULT_OK = -1; // Defined by Android. You don't write this code.
public static final int RESULT_CANCELED = 0; // Defined by Android. You don't write this code.
public static final int RESULT_ENDED_GAME = RESULT_FIRST_USER + 0; // Defined by an app.
public static final int RESULT_ACTIVATED_RADAR = RESULT_FIRST_USER + 1; // Defined by an app.
public static final int RESULT_LAUNCHED_ROCKETS = RESULT_FIRST_USER + 2; // Defined by an app.
Related
I'm trying to get the current from the API. There's a function at the bottom of the documentation (https://developer.android.com/reference/android/os/BatteryManager.html) called getIntProperty, so I presume that'd be the function I'd need. However, up to now I've been simply using the getIntExtra function on an ActionBatteryChanged intent, and that same method doesn't work for this. What do I need to do different?
That function is used to get the current value, not get updates like you do with a Broadcast and an Intent. You'd call batteryManager.getIntProperty(BATTERY_PROPERTY_CURRENT_NOW) to get the value. Any of the BATTERY_PROPERTY_X constants could work, but not every phone supports all of them.
A more comprehensive code samples:
Declare this at the class level
private BatteryManager mBatteryManager = null;
onCreate()
{
mBatteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE);
}
Create a timer and inside the timer routine, add this
int mBatteryCurrent = mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
The current is in uA unit. a positive integer means drawing current out of
battery and a negative integer means charging current into battery.
I still am a beginner in Android development and will try to make my question as clear as possible with a schema of what I have in my mind.
Purpose of this application:
- I want the user to have the choice between a few buttons and when clicking on any of them, it would open a list view with different content according to the button.
ex : if you click on "Category_1" button, only elements with a fitting id will appear in the listview.
So far, I have :
- defined my "handler" class (extends SQLiteOpenHelper) : name/path of DB, definition of CRUD, .getReadableDatabase, etc.
- define a class for my table, in my case "Restaurants.java" with getters/setters and constructor.
- defined my MainActivity with empty listeners for my button.
- defined my "DatabaseAdapter.java" in which I want to define the methods/sql requests which will communicate with the database and get the information I want from it.
- defined my ListViewActivity with nothing to display so far.
Here is a schema of what I want with the idea of how to make it to try to optimize my application :
To sum up:
- I want a listener for each button setting a variable to a certain value (for example: "if you click on 1 then set the value of A to 1") and opening the ListViewActivity.
- There would be a method defined in "...Adapter.java" sending a request to the database and having the variable A defined earlier as an input.
- And then, when clicking on the button, the ListViewActivity will open and call the method from "..Adapter.java", and finally display the results.
So, here are my questions :
- First of all, is the design optimized enough to allow my application to run fast? I think it should as all the button open only one activity and there is only one method defined for all buttons.
- I have a hard time defining the method in "...Adapter.java" which will be called from my ListViewAcitivity. The input should be the variable obtained when clicking on the button but I don't really know how to get a variable in one activity, open a second activity where to display results by using the variable in a third activity... :s
Is it fine to set a variable to a certain value when we click on a button and use this variable in another class as an input for a method?
public findNameInTable(int A){
string sql = " select NAME from MY_TABLE where CAT1 = " + A;
c = database.rawQuery(sql, null); }
Thanks in advance for any indications, suggestions or links which could help me to make my application come true, and sorry if some questions really sounds newbie, I am starting !
Have a good day !
Part 1: The best way I have found to pass variables to other activities is with a putExtra(String, variable);. Say you change the variable name on a button press, you can then call:
YourNewActivityClassName var = new YourNewActivityClassName();
Intent i = new Intent(context, YourNewActivityClassName.class);
i.putExtra("name", name);
startActivity(i);
Then in the activity you just created, you can call this in the onCreate method:
Intent i = getIntent();
final String name = i.getStringExtra("name");
Of course this is assuming the variable was defined as a String before the putExtra was called.
If you want to use other variable types, there are different get***Extra commands you can call like getIntExtra(int, defaultval) but the putExtra will still be used to send it.
Part 2: For calling a method with a variable assigned in a button click, I have found the best way to do this is with a "holder class" this holder can be defined as a final, and a button press assigns a value to one of it's slots. Here is my holder for Integers:
public class holder {
int to;
public void setTo(int to){
this.to = to;
}
public int getTo(){
return to;
}
}
I instantiate my class as final within my on create final holder hold = new holder();
then call my hold.setTo(int); within a list click listener. When I want to get the data, I simply call hold.getTo(); and I have my integer.
Heres a similar post: Pass value outside of public void onClick
Hope this helps!
Mike
https://stackoverflow.com/a/2508138/1508448
Please go through the link above.
My compiler is showing a problem in REQ_CODE_PICK_IMAGE.
It asks if I need to create a variable. What is it?
//you need to create an variable in your class
public final static int REQ_CODE_PICK_IMAGE=1
switch(case)
case 1: //instead of hardcoding here you need to declare the variable as integer
break;
case 2;
.
.
EDIT:
When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), along with the integer identifier it originally supplied.
Im going a little crazy with this. In my app, i take a string which represents a bus stop, and then have an algorithm that matches it and displays its schedule. I needed to make that window an activity instead of a dialog and am using intents. Heres my code to send the intent:
Intent intent = new Intent(context, StopDialogActivity.class);
intent.putExtra("stop name", stopName);
context.startActivity(intent);
and heres my code to retrieve the string (in my onCreate):
Bundle extras = getIntent().getExtras();
departureStopName=extras.getString("stop name");
The string displays properly, but it isnt equal to a test string i have which is the same stop. The intent sends an integer over correct, what am i doing wrong with processing strings?
Make sure when comparing strings to use testName.equals(stopName) and not testName == stopName.
Using .equals() uses the equals method in the String class which compares the content. Using == compares the String Objects themselves, which need to be the same object in memory to evaluate to true.
Ah, my problem was while I use .equals() as a test to see if it was coming through okay, i was using == in the part of my code that broke.
How can I measure time that ListView/ListActivity takes for rendering its list?
I have a ListActivity that takes a long time to show (on older devices it's ~3s) and I'd like to optimize that.
EDIT:
I already have time between Activity1 and Activity2. During this time, Activity2 is doing some stuff (initializing). Among other things, it renders its list. I want to get time this activity takes to render that list. If total time between activities is 3s, I want to know whether rendering list takes 2.9s or 0.5s....
You could simply ouput the time. For example you could use the logcat
final long t0 = System.currentTimeMillis();
// code to measure
Log.w(TAG, "TEXT" + System.currentTimeMillis()-t0);
Of course you could use any other system for the ouput like a dialog or stuff. Just use what you like.
EDIT:
If you don't want to use a debug message in your code all the time you could do it like this:
Create a class called settings:
public class Settings {
public static final boolean DEBUG = true;
// If you prefer you could do use an enum
// enum debugLevel {SHOW_EVERYMESSAGE, ERRORS, IMPORTANT_MESSAGES, ...}
// In your classes you would have to check that DEBUG is less or equal than
// the debugLevel you want
}
In classes where you want to use a debug message simply do this
import xxx.yyy.Settings
class foo {
final static boolean DEBUG = Settings.DEBUG;
if(DEBUG){
// Debug messages
}
}
Now if you want to disable DEBUG messages you could simply set DEBUG = false in your Settings class.
If you want to measure between two activities you could use intents and send t0 with an intent to the other activity to compute the time. Of course you could include this with if(DEBUG){ /* code */ } statements to spare the sending of the intent in the final release. The if statements should not increase the computation of your code too dramatically.
I cannot tell if Java offers a better implementation using System.currentTimeMillis() or System.nanoTime(). Nevertheless, you should give the TimingLogger class a try. Take a look at this article describing the usage of the TimingLogger helper class.