I have two activities:
ListViewActivity
AddActivity
and one object class :
todoObj
I have a list view in my ListViewActivity activity and when I click add button this will initiate AddActivity . In AddActivity when user enters Title, choose date, time and the category I want AddActivity to create a todo object and pass this back to the ListViewActivity.
Sorry I could not share the code itself, it always gives this error all the time so I uploaded on github please check it out.
Sorry again.
Many thanks.
All you have to fix onActivityResult method in ListViewActivity:
Because of you setting result code ADD_REQUEST_CODE in AddActivity "setResult(ListViewActivity.ADD_REQUEST_CODE, intent);", you should use "if (resultCode == ADD_REQUEST_CODE) {" in ListViewActivity not RESULT_OK.
You should receive intent from onActivityResult not ListViewActivity's intent. getIntent() gives ListViewActivity's intent. So use data variable:
onActivityResult(int requestCode, int resultCode, Intent data)
Final code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_REQUEST_CODE) {
if (resultCode == ADD_REQUEST_CODE) {
Log.i("ListViewActivity", "Returned onActivityResult");
TodoObj todoObj = (TodoObj) data.getParcelableExtra("EXTRA_TODO");
Toast.makeText(ListViewActivity.this, "" + todoObj.getmYear(),
Toast.LENGTH_SHORT).show();
}
}
}
Related
My MainActivity has an EditText with the hint "Weight (lb)", as well as a button that goes to the SettingsActivity. In SettingsActivity, the user is able to change the units used from US to metric. Upon exiting SettingsActivity via the built-in back button on the phone, I want the hint for the EditText to immediately change from "Weight (lb)" to "Weight (kg)".
The farthest I've gotten is using the onBackPressed() method in SettingsActivity. The button press is detected, and the code inside it is executed, but I don't want to change anything related to MainActivity's inside the SettingsActivity class.
Is there an on___() method that I should be using here that I don't know about? Any help would be appreciated.
You can use startActivityForResult for SettingsActivity and after that handle the return result as:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
// 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)
}
}
More detail: https://developer.android.com/training/basics/intents/result.html
You can use startActivityForResult for SettingsActivity and after that handle the return result as:
In your MainActivity:
public static int CALL_SETTINGS = 1000;
startActivityForResult(new Intent(this,SettingsActivity.class),CALL_SETTINGS);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
// 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)
}
}
In your Settings Activity:
on backpressed write this:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",data);
setResult(Activity.RESULT_OK,returnIntent);
finish();
Easy option is to store the waitUnit in shared preference and on OnResume() of the Main activity read it from there and update the widget.
You can update the preferences in your Settings activity like below:
preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key),
Context.MODE_PRIVATE);
preferences.edit().putString("unit", "kg");
And you can read this changed value in OnResume() of your Main Activity
preferences.getString("unit", "default unit")
Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.
So, I've read some threads about this, but I can't get this to work.
Basically I have this Dialog in which the user chooses to take a new pic or select a pic from their gallery. Here is the code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true)
.setItems(R.array.galeria_camera_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int userChoice) {
if (userChoice == 1) {
// take photo
}
if (userChoice == 0) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
}
}
});
return builder.create();
}
And then, the onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//choose file from gallery
}
Can anyone help? Before I have to re-read 10 pages worth of theory again... I'm quite new to this kind of things (onResult). Thank you.
Try implementing something like this in the activity where the dialog fragment is fired from:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId);
fragment.onActivityResult(requestCode, resultCode, data);
The underlining activity is what will receive onActivityResult, so u should be able to 'point' it towards onActivityResult of the dialog fragment.
The easy way to fix this is to move your fragment's onActivityResult into your activity, or you can use your activity's onActivityResult to call a method in your fragment.
I looked at my code and apparently I managed to get around this by calling this.getActivityForResult from my fragment, and then the result returned to my fragment, but I'll be honest with you: I don't remember writing this.
Ok, these two things made it work:
getActivity().startActivityForResult(galleryIntent, 1); You have to add getActivity()
As you guys said, you have to declare the onActivityResult() in the Activity itself.
Thanks guys!
I currently started working on Android and right now i am trying to figure out how to send an Intent From a 2nd Activity to the first!
Now i know that i have to use startActivityForResult and i think i actually used that the right way.
However there is a list on my Main Activity, that i want to fill with a String from my 2nd Activity. But i cant get the List to update without getting a NullPointer Exception when trying to add the received String to the List.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
I think my issues lay in some Activity Order issues. How do i send the receivedString from OnActivityResult to OnResume once the 2nd activity is finished().
Or The Actualy String coming over from my 2nd activity is always empty for some reason, but i cant figure out why that would be the case...
ANSWER FOUND CHECK POST Beneath
Your problem here is that you're not getting the right intent in the result. Change:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//check for right result
if (requestCode == 10) {
if (resultCode == RESULT_OK) {
//Catching name from 2nd Activity
Intent GoToMain = getIntent();
To:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//check for right result
if (requestCode == 10) {
if (resultCode == RESULT_OK) {
//Catching name from 2nd Activity
Intent GoToMain = data;
How to use startActivityForResult outside an activity context say in a regular java class
EX:
In the main activity, Im calling method of SampleClass. In this method i have to open the device camera and obtain the image and process it.
The problem is the control is not going to the onActivityResult callback method inside the SampleClass but instead it is going to onActivityResult callback method inside the MainActivity. What am i doing wrong?
I need to be able to handle the image inside Sample Class only. How can i achieve this
Code Snippet
MAIN ACTIVITY:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
SampleClass sample = new Sample(this);
sample.openCamera();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
syso("INSIDE MAIN ACTIVITY");
}
if(resultCode == RESULT_CANCELED){
...
}
}
}
}
SAMPLE CLASS:
public class SampleClass extends Activity{
private Context context;
public SampleClass(Context context){
this.context = context;
}
public void openCamera(){
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) context).startActivityForResult(photoIntent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
}
If i run this program, The output on the console is INSIDE MAIN ACTIVITY. What i want is to be able to get INSIDE SAMPLE CLASS as the output.
Thanks and Regards
Instead of using
SampleClass sample = new Sample(this);
sample.openCamera();
just use
Intent intent = new Intent(this, SampleClass .class);
and write SampleClass onCreate call openCamera().
Hope this will help.
the problem is that you open the camera using MainActivity Context!!
sure will triggered onActivityResult() of MainACtivity.
((Activity) context).startActivityForResult(photoIntent, 0);
see context object = MainActivity check you SimpleClass constructor.
public SampleClass(Context context){
// when you call from mainActivity and send **this** it means context = MainActivty
this.context = context;
}
if you want SampleClass onActivityResult() triggered there is no way else extends Activity
then call it like normall way ....
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
context.startActivityForResult(photoIntent, 0); // context must = SampleClass (); no other way
but if you still want to handel image in SimpleClass then follow this code remove extends Activity
in SimpleClass
public static void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
and in you MainActivity class
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SimpleClass.onActivityResult(requestCode,resultCode,data);
}
never ever create new object from activity using new key word Adnroid OS will do that for you when you say
Itnent foo = new(context,foo.class);
and if you did maybe this means hack Code and android OS.
i hope you understand and hope this helps
It is impossible. onActivityResuly will be called on that activity, which have ran another activity.
So if you took activity1 instance to run another activity, another activity will call activity1.onActivityResult()