I know the listeners are supposed to make life easier, especially in a multi-tasking environment like android, but sometimes (to me) it just makes things harder.
Right now I have an ExpandableList activity which presents a list of events, and I want the user to select the group & child of interest and select a notification sound that will play when that event happens. So the list is set up and I have a setOnChildClickListener set up which runs my SetRingtone method:
protected void SetRingtone(int groupPosition, int childPosition) {
Intent intent = new Intent( RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_ALL);
intent.putExtra( RingtoneManager.EXTRA_RINGTONE_TITLE,
"Select Tone");
startActivityForResult( intent, PICK_NOTIFICATION_SOUND);
}
So that method has access to the selected group and child positions. The problem is that to get the ringtone selected from the ringtone selector, you have to set up another lister, onActivityResult:
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri =
data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
String ringTonePath = uri.toString();
ExpandableListView variableView =
(ExpandableListView)findViewById(android.R.id.list);
int p = variableView.getSelectedItemPosition();
Log.d("phca", "The selected item number was: " +
p + "; The sound selected is: " + uri );
}
}
}
And now I don't have access to the selected group and child. As you can see, I tried to get the position from the getSelectedItemPosition, but it always returns -1 here.
I know I am probably making this harder than it really is.
You could just store the group and child in instance variables before the call to startActivityForResult and then use these instance variables in onActivityResult.
Did you try to use variableView.getSelectedItem()?
If this return null so it mean that something wrong with your list
Related
In my android app I want to change the input method. So I start a new Activity which shows the language settings in the device. Then user can change it. However then I want to know that if the user has changed it. So I wrote a function for that also. My code so far is...
Intent enableIME = new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
startActivityForResult(enableIME,0);
if(isInputMethodEnabled()){
activateshadow.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_pressed));
activateshadow.setText("Deactivate Shadow");
prefs.edit().putBoolean("Activate", false).commit();
}else{
Toast.makeText(MainActivity.this,"You haven't change the input method to simpleIME.In order to activate you must change it.",Toast.LENGTH_SHORT).show();
}
my is inputMethodEnabled function is....
public boolean isInputMethodEnabled() {
boolean isIME ;
String id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
String [] name = id.split("/.");
// Toast.makeText(MainActivity.this,"s:"+name[1]+":s",Toast.LENGTH_SHORT).show();
if(name[1].contains("SimpleIME") ){
isIME = true ;
}else{
isIME = false;
}
// Toast.makeText(MainActivity.this,"Returning..."+isIME,Toast.LENGTH_SHORT).show();
return isIME;
}
if(isInputMethodEnabled()) always fails because when the new intent(settings) opens and it take some time to change the input method to simpleIME . How to fix this problem?
You catch when a launched Activity returns in onActivityResult. The requestCode you supplied to startActivityForResult will be a parameter, as will the Activity's result. The Activity may also set other data which you didn't ask about.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 555) {//555 is the intent ID you gave in startActivityForResult(enableIME,555);
if (resultCode == /*Result1*/)
//Do something
else {
//Do something else
}
}
}
You need a unique id when calling startActivityForResult(enableIME,0);
startActivityForResult(enableIME, 555);
Better still replace 555 with a named variable.
if u look at android life cycle, when activity is finished whole android call onDestroy() method.
so u can call and override this method.
just need write:
#override
protected void onDestroy(){
// code
super.onDestroy();
}
u can manage and override all of life cycle's parts in android
e.g: onResume to get current activity
i hope this help u
I'm creating a dynamic form activity and I get the instructions on how to make the forms from a db. The activity essentially just scrolls the list of questions and sees what type they are and adds them. So it's an activity that adds a view as a question. That all works fine by itself. I tried to keep the responses/questions inside the specific question classes which just subclass a base question.
The problem I'm having is when I try to add a camera "question" to prompt the user to take a picture, I can't get the result inside the view. I managed the launch the activity in the view, and it returns the result to the questionnaire activity. The activity doesn't know which question it's meant to add it to, since it's all done dynamically. So I tried passing through the questionId through as an extra in the camera intent and receive it in the questionnaire activity where it then scrolls through the questions it's added and if it's the same one, it adds the picture to the question it's associated to.
The way it adds questions is by having a viewgroup which just inserts at a part for each of them.
This the relevant part that launches the camera (I've tried using it without a bundle, too). This is inside a subclass of a BaseQuestion which is just a subclass of a linearlayout:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Bundle bundle = new Bundle();
bundle.putInt("questionId", getQuestionId());
intent.putExtras(bundle);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "image.tmp")));
((Activity)getContext()).startActivityForResult(intent, TAKE_PHOTO);
This is the relevant part that handles the result which is in an activity.
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
Bitmap image = null;
switch (requestCode)
{
case TAKE_PHOTO:
{
if (resultCode == RESULT_OK)
{
InputStream inputStream = null;
File file = new File(Environment.getExternalStorageDirectory(),
"image.tmp");
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (inputStream == null) {
Uri uri = imageReturnedIntent.getData();
try {
inputStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
image = createScaledBitmapFromStream(inputStream, 200, 300);
Bundle bundle = imageReturnedIntent.getExtras();
if (bundle != null)
{
int questionId = bundle.getInt("questionId");
for (BaseQuestion questionView : questionViews)
{
if (questionId == questionView.getQuestionId())
{
questionView.setResponse(image);
}
}
}
}
else if (resultCode == Activity.RESULT_CANCELED)
{
Log.d("something", "something");
}
break;
}
}
}
EDIT: Solved, might not be clear from answer
I made questionIdForResult in my superclass which I set to -1. I then set it to the questionId when it runs startActivityForResult, and then in onActivityResult, I check each question if their questionIdForResult matches their questionId and if it does, use that one. I then set it back to -1 to make sure if you have two, it doesn't go to the other one.
Your approach will not work, as there is no requirement for any camera app to magically copy extras from the incoming Intent to the result Intent. In fact, I will be rather surprised if any camera activity does this.
You could store questionId() in a data member of the activity (e.g., questionIdForTheNextActivityResult), then use that value in onActivityResult(). Bear in mind, though, that taking a picture using a third-party app means that your process may be terminated before you get control again, so be sure to save that data member via onSaveInstanceState() and restore it via onRestoreInstanceState().
I have an activity that shows a ListView, the ListView is composed by a TextEdit and a Button.
The button is used to pick the email of a contact and put it in the TextEdit.
The listener of the button is created in my custom array adapter and from this method I'm calling the activity to pick the contact like this:
Intent intent =new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Email.CONTENT_URI);
((Activity) v.getContext()).startActivityForResult(intent, MainActivity.act_pick_contact_mail);
I've created the method onActivityResult in my activity and I can get the email picked by the user, but I don't know from wich position the button was pushed.
This is the code I've written:
Do you know how can I do it ?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Pick an email
if(requestCode==MainActivity.act_pick_contact_mail){
try{
if(resultCode==Activity.RESULT_OK){
Uri uri=data.getData();
Cursor emailCur=getContentResolver().query(uri,null, null, null,null);
emailCur.moveToFirst();
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
//Change the first item, but I need to know the real position
contactos.set(0, new Contacto(email));
adapter.notifyDataSetChanged();
emailCur.close();
}}
catch(Exception e){
Toast.makeText(getBaseContext(), "Email wasn't found", Toast.LENGTH_SHORT).show();
e.getCause();
}
}
}
Thanks in advance.
EDIT: Solved creating an attribute within the adaper with a getter.
I see two options here.
One, save the position in a class attribute of your activity. Then reference it when onActivityResult returns.
Two, use the position as your request code. Eg replace MainActivity.act_pick_contact_mail with the position. Then in onActivityResult assume a request code of >= 0 is valid.
I have a ListActivity which displays all the aggregate contacts. When the user clicks one, my code calls startActivityForResult. All this works properly. When the user finishes editing, I want my ListActivity to be displayed again. Instead, the "people" activity gets displayed. Similarly, my onActivityResult function never gets called.
Here is the code handling the click:
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
Cursor cur = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
cur.moveToPosition (position);
String key = cur.getString (2); // "2" is the col containing the LOOKUP_KEY
System.out.println ("clicked " + key);
// make intent to edit contact
Intent intent = new Intent (Intent.ACTION_EDIT);
intent.setData (Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key));
startActivityForResult (intent, 2);
}
And I also have an onActivityResult function:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
System.out.println ("request " + requestCode + ", result " + resultCode);
}
Any suggestions?
I filed a bug to android about this. Someone looked at it and responded that there is an undocumented workaround. From the bug report:
The undocumented workaround is to call putExtra("finishActivityOnSaveCompleted", true); on the ACTION_EDIT Intent.
However, as this is undocumented, I have no idea which Android version(s) will use it.
I tried it and it works for the version of Android I'm using: 4.1.2. See issue 39262 for more info.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 2) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
Replace your onActivity result to this. for request code get after
they will work
Add
super.onActivityResult(requestCode, resultCode, data);
in OnActivtyResult().
Make sure you are calling startActivityForResult from an activity, then only your onActivityResult will be called. For example, if you have the similar code in an fragment, the onActivityResult will never be called.
As of right now, in my app I have created a rudimentary gallery app using the provided widget, I need this to select a picture from the phone. This is working fine and everything, but lacking very much in presentation.
I've got a couple apps on my phone that do the same thing, but they somehow use the gallery that's already in the phone to let the user select an image. FourSquare, for example, when you select an image to use as your picture, it loads the gallery and asks you to select an image.
How is this possible? I've scoured the internet for the last couple and have come up empty handed.
To get an image from the standard gallery you can do:
private static final int MEDIA_IMAGE_REQUEST_CODE = 203948; // This can be any unique number you like
Intent getImageFromGalleryIntent =
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(getImageFromGalleryIntent, MEDIA_IMAGE_REQUEST_CODE);
Then to receive the image once the user has chosen one:
protected final void onActivityResult(final int requestCode, final int resultCode, final Intent i) {
super.onActivityResult(requestCode, resultCode, i);
if(resultCode == RESULT_OK) {
switch(requestCode) {
case MEDIA_IMAGE_REQUEST_CODE:
// Get the chosen images Uri
Uri imageUri = i.getData();
// Load the bitmap data from the Uri
// It is probably best to do this in an AsyncTask to avoid clogging up the Main Thread
break;
}
}