how to set data into dialog-fragment from mapActivity - android

i m fetching lat-lang data on longclick event on my map. the problem is when i pass data in my dialog-fragment's edittext, its getting null,
here is my code of long-click event
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener, View.OnClickListener {
#Override
public void onMapLongClick(LatLng point) {
if(mMap != null) {
mMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
double latitudeNew = point.latitude;
double longitude = point.longitude;
mLatLongArray.add(new double[]{latitudeNew, longitude});
isMarkerClicked = false;
lat = String.valueOf(latitudeNew);
logt = String.valueOf(longitude);
Toast.makeText(getApplicationContext(),lat,Toast.LENGTH_SHORT).show();
}
}
}
and i m going back using using onbackpress in my button click like this.
AddGeofenceFragment.ViewHolder fragment = new AddGeofenceFragment.ViewHolder();
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnBackToFragment) {
String TAG = "com.rockstar.googlemap.AddGeofenceFragment";
onBackPressed();
// getSupportFragmentManager().beginTransaction().replace(R.id.dialogFrgment, new AddGeofenceFragment()).addToBackStack(TAG).commit();
fragment.latitudeEditText.setText(lat);
fragment.longitudeEditText.setText(logt);
}
}
here is my workflow, first i open my dialog-fragment on fab-button click,
and open map for get latitude and logtitude data,
mow when i click on my "goBackTOFragment" button my app unfortunately stop,
i dont understand wht its happening, even i get data on longclick event, i checked using toast, but its not passing to the fragment. it says null pointer exception on this line
fragment.latitudeEditText.setText(lat);
fragment.longitudeEditText.setText(logt);
i want to set both string data lat and logt in my previous dialog-fragment's edittext, i dont know how to pass data in fragment how do i do that. please help me. i really appreciate that.

If you use the map activity just getting lat/lng, it could be started like this
Intent intent = new Intent(this, MapsActivity.class);
startActivityForResult(intent, 1);
And then when you press back button (MapsActivity)
Intent position = new Intent();
position.putExtra("lat", lat);
position.putExtra("lng", lng);
setResult(1, position );
finish();
Again your first activity, you received position from map. U can use dialog fragment's fields. In your case, you losing dialog fragment instance.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == 1){
double lat = intent.getDoubleExtra("lat", -1);
double lng = intent.getDoubleExtra("lng", -1);
}
}
I think, this approach better for your case. Also you can check deeply from here
EDIT
I also saw your latest update on the question post.
AddGeofenceFragment.ViewHolder fragment = new AddGeofenceFragment.ViewHolder();
#Override
public void onClick(View v) {
...
fragment.latitudeEditText.setText(lat);
fragment.longitudeEditText.setText(logt);
}
This wrong, with this usage, you just instantiating, not calling onCreateView method of DialogFragment. Check this below
In first activity, i'm calling AddGeofenceFragment first time. Also keeping fragment instance for later usage.
FragmentManager fm = getSupportFragmentManager();
AddGeofenceFragment dialogInstance = new AddGeofenceFragment();
AddGeofenceFragment.show(fm, "AddGeofenceFragment");
In Maps activity, I choose position.
public void onMapLongClick(LatLng point) {
Intent position = new Intent();
position.putExtra("lat", point.latitude);
position.putExtra("lng", point.longitude);
setResult(1, position );
finish();
}
In first activity, i'm getting the position
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == 1){
double lat = intent.getDoubleExtra("lat", -1);
double lng = intent.getDoubleExtra("lng", -1);
dialogInstance.latitudeEditText.setText(lat);
dialogInstance.longitudeEditText.setText(lng);
}
}

Main activity and fragment should communicate over interface callbacks. They generally has no common memory to share directly. See http://developer.android.com/intl/es/training/basics/fragments/communicating.html

Related

Why Activity does not provide onActivityResult(...) callback as it does for permissions (onRequestPermissionsResult)?

Intro
I am writing class that provides me current localization only once if its able to, and returns default coordinats if can not do it for any reason. Class will be used in many places so I wanted to make it as simple to implement for others as it can be. So code would look like
LocationFinder locationFinder = new LocationFinder(this);
locationFinder.setLocationResolvedCallback(new LocationFinder.LocationResolvedCallback() {
#Override
public void onLocationFound(double latitude, double longitude) {
Log.e("Activity", latitude + " " + longitude);
}
});
locationFinder.findCurrentLocation();
but there is a case when locations settings are turned off, and I have to request user to turn them on.
task.addOnFailureListener(activity, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case CommonStatusCodes.RESOLUTION_REQUIRED:
try {
// Show the dialog and check result
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(activity, REQUEST_LOCATION_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
So now I have to go into activity and #Override onActivityResult(), handle it from activity level which makes my class less independent.
I wrote method that handle result code within my LocationFinder
public void onActivityResult(int requestCode, int resultCode) {
if (REQUEST_LOCATION_SETTINGS == requestCode) {
if (resultCode == Activity.RESULT_OK) {
requestCurrentLocation();
} else {
locationResolvedCallback.onLocationFound(DEFAULT_LAT, DEFAULT_LONG);
}
}
}
but I still have to #Override any activity that uses this class and call this method from it.
So my question is.
Why does android architects did not provide onActivityResult(...) callback as they did in case of permissions? So i could handle any request from within a class by just having reference to activity like
activity.onActivityResultCallback(...){///code}
There must be a reason but I may be missing something very obvious.
Activities have a lifecycle, and are therefore instantiated in a different way from an ordinary class.
However the functionality you are looking for can be provided by startActivityForResult(Intent). After the started activity is finished or removed from the stack, an intent is passed hack to the calling activity with a bundle you can use to pass back information
onActivityResult() called when you start activity for the result from the current Activity.
if you set the onActivityResult callback somewhere else following will happen.
Your fragments which are hosted in the activity can't get onActivityResult callback.
If you call multiple activities for the result you can't get a result properly (in one place).
For your reference see the below code. This will be never executed if you not called super.onActivityResult().
FragmentActivity onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFragments.noteStateNotSaved();
int requestIndex = requestCode>>16;
if (requestIndex != 0) {
requestIndex--;
String who = mPendingFragmentActivityResults.get(requestIndex);
mPendingFragmentActivityResults.remove(requestIndex);
if (who == null) {
Log.w(TAG, "Activity result delivered for unknown Fragment.");
return;
}
Fragment targetFragment = mFragments.findFragmentByWho(who);
if (targetFragment == null) {
Log.w(TAG, "Activity result no fragment exists for who: " + who);
} else {
targetFragment.onActivityResult(requestCode & 0xffff, resultCode, data);
}
return;
}
Due to this factors android not provided anonymous callback of onActivityResult()
Suggestion: In my case, I have used BroadcastReceiver to get onActivityResult on my class.
Hope it helps:)

Starting multiple activities and getting result before starting the next one

I am making a quiz app, my MainActivity(main menu) launches QuestionActivity using startActivityForResult, in QuestionActivity (Question text and answer buttons). After the user has answered the question, I want to send a boolean back a to MainActivity to update the score which then can be pushed into the next intent, in the Question Activity, I display the score in the Actionbar.
The problem is when I answer one question, setResult and Finish Runs but onActivityResult does not, after I answer all questions then OnActivityResult runs 10 times.
How can I get onActivityResult to run after I answer each question, not at the end?
Do I need to use intent flags?
Extra Info
In MainActivity, when the user starts the quiz:
//Called when user clicks quiz
//Creates the list of questions and then asks them.
public void makeQuiz(View view) {
//Pick the questions for the quiz
question[] quiz = new question[10]; //A quiz with 10 questions
for (int i = 0; i < quiz.length; i++) {
quiz[i] = myDBHelper.pickQuestion();
askQuestion(view, quiz[i],i,qscore);
Log.d("Asked question", Integer.toString(i));
}
}
Ask Question is used to start the QuestionActivity:
//Creates a question and then passes it though to the question view.
public boolean askQuestion(View view, question q, int questionNum, int qscore){
question q1 = q;
Log.d("Correct Ans",q.CorrectAns);
Intent question = new Intent(this, QuestionActivity.class);
Bundle extras = new Bundle();
extras.putString("QUESTION", q.QuestionText);
extras.putString("MODULE", q.Module);
extras.putString("CORRECT_ANS",q.CorrectAns);
extras.putString("ANS1", q.WAns[0]);
extras.putString("ANS2", q.WAns[1]);
extras.putString("ANS3", q.WAns[2]);
extras.putInt("qscore",qscore);
question.putExtras(extras); //Passing the question to the QuestionActivity
startActivityForResult(question,1);
return true;
}
In QuestionActivity, When the user answers the question correctly:
//Pass back that we got the correct answer
resultIntent = new Intent();
resultIntent.putExtra("ANSWER",true);
setResult(1, resultIntent);
Log.d("True", "Set result has been called");
finish();
Back in MainActivity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
//Check which event we are responding to
Log.d("onActivityResult", "called"); //This never runs
if(resultCode == 1){
//Do something with the intent
//if q is correct, update the score in shared prefrences,
Boolean result = data.getBooleanExtra("ANSWER",false);
Log.d("ANSWER IS ", Boolean.toString(result));
qscore += result ? 1:0; //This updated score is then pushed into the next intent so it can be displayed in the next question activity.
}
}
Alright so this is your problem you start the activity for result with:
startActivityForResult(question,questionNum);
so questionNum is your requestCode
but when you finish the QuestionActivity you finish it like this:
setResult(Activity.RESULT_OK, resultIntent);
so here your request code is the value of Activity.RESULT_OK
you need them to be equal.
Edit:
for your request in the comment look at this:
private static final int REQUEST_CODE = 123131;
private Stack<Intent> intentStack = new Stack<>();
//Called when user clicks quiz
//Creates the list of questions and then asks them.
public void makeQuiz(View view) {
//Pick the questions for the quiz
question[] quiz = new question[10]; //A quiz with 10 questions
for (int i = 0; i < quiz.length; i++) {
quiz[i] = myDBHelper.pickQuestion();
askQuestion(view, quiz[i], i, qscore);
Log.d("Asked question", Integer.toString(i));
}
startActivityForResult(intentStack.pop(), REQUEST_CODE);
}
//Creates a question and then passes it though to the question view.
public boolean askQuestion(View view, question q, int questionNum, int qscore) {
question q1 = q;
Log.d("Correct Ans", q.CorrectAns);
Intent question = new Intent(this, QuestionActivity.class);
Bundle extras = new Bundle();
extras.putString("QUESTION", q.QuestionText);
extras.putString("MODULE", q.Module);
extras.putString("CORRECT_ANS", q.CorrectAns);
extras.putString("ANS1", q.WAns[0]);
extras.putString("ANS2", q.WAns[1]);
extras.putString("ANS3", q.WAns[2]);
extras.putInt("qscore", qscore);
question.putExtras(extras); //Passing the question to the QuestionActivity
intentStack.push(question);
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Check which event we are responding to
if (resultCode == RESULT_OK) {
//Do something with the intent
//if q is correct, update the score in shared prefrences,
Boolean result = data.getBooleanExtra("ANSWER", false);
Log.d("ANSWER IS ", Boolean.toString(result));
qscore += result ? 1 : 0;
if(!intentStack.isEmpty()){
startActivityForResult(intentStack.pop(), REQUEST_CODE);
}
}
}

Finish Activity not close in Android (Xamarin)

I have Two Activity one is InventoryActivity and Second is StoneDetailActivity. In my InventoryActivity have RecycleView In RecycleView Button Click I start the StoneDetailActivity using StartActivityForResult below code.
Intent stonedetailIntent = new Intent(context, typeof(StoneDetailActivity));
stonedetailIntent.PutExtra("SearchitemObject", stoneJson);
stonedetailIntent.PutExtra("position", position);
context.StartActivityForResult(stonedetailIntent, 1000);
context.OverridePendingTransition(Resource.Animation.Slide_in_right, Resource.Animation.Fade_back);
In StoneDetailActivity Button click I use this code to Finish the current Activity and go to OnBackPressed().
public override void OnBackPressed()
{
Intent intent = new Intent();
intent.PutExtra("BoolCheck", postflag);
intent.PutExtra("Position", position);
SetResult(Result.Ok, intent);
Finish();
}
and In InventoryActivity I have set this code.
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
bool checkflag = data.GetBooleanExtra("BoolCheck", false);
int position = data.GetIntExtra("Position", -1);
if (checkflag && position > -1)
{
searchItems.RemoveAt(position);
inventAdapter.NotifyDataSetChanged();
txt_totalStone.Text = searchItems.Count.ToString();
txt_totalCarat.Text = searchItems.Sum(c => c.Weight.Value).ToString();
txt_totalAmount.Text = searchItems.Sum(c => c.Rate.Value).ToString();
mainActivityBool = true;
badgeCounttextView.Text = BizApplication.BADGE_COUNT.ToString();
}
}
}
Button Click code :
add_to_cart_button.Click += async (sender, e) =>
{
ProgressDialog pdialog = new ProgressDialog(this);
pdialog.SetMessage("Please Wait...");
pdialog.Show();
cartItem = new CartItem();
cartItem.StoneId = searchItem.PacketId;
cartItem.UserId = BizApplication.getCredential().Id;
cartItem.Amount = searchItem.Rate.Value;
cartItem.Discount = searchItem.Discount;
postflag = await InventoryService.AddToCart(cartItem);
if (postflag)
{
OnBackPressed();
BizApplication.BADGE_COUNT += 1;
}
pdialog.Dismiss();
};
this code work fine for first Time. But Again if I do the same process, the StoneDetailActivity set open eventhough if I click finish.
UpDate :
When I full debug my code and i found that when I click on Second time OnBackPressed(). and Finish it my debug again start the OnCreate activity that's why it happening. But I am not starting Again then Why is Happening.
What happen I don't understand. Any Help be Appreciated.
As per this Post the Problem was that inside ListView or RecycleView if we are perform some task like OnclickListener then we have check is OnclickListener like below way other it will fire multiple event.
if (!button.HasOnClickListeners)
{
button.Click += this.clickHandler;
}
Then the code is working fine.
For more detail visit this :
https://forums.xamarin.com/discussion/9244/single-click-on-button-invoking-multiple-clicks
try by adding flag Intent
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);

Correctly load data in listview from startactivityforresult

GOAL
What I am trying to do:
Click on my search button and my database is queried with the results passed in. If nothing is found, we are taken to an activity which says so, but if results are found they are loaded into a list.
What I have done
When I click on the search button I call startActivityForResult then this intent calls an activity (whose layout consist of a list_view). The search button also pass along my parameters and query my database.
if there are no results then an activity saying "No Records" is displayed"
and if there are records the else condition is true and the records are loaded in the list
PROBLEM
The problem I am experiencing is, when the list is loaded, if I want to go back to my search form, I must press the back button a total of three times. I am not entirely sure but I believe this strange behavior is stemming from me not returning a result to the started activity when the else clause is invoked.
I have placed what I think is the important part of my code below, would appreciate any assistance
Main Activity
private void startStudentQuery() {
Intent intent = new Intent(getBaseContext(), retrieveStudentData.class);
intent.putExtra("firstname", firstname);
intent.putExtra("lastname", lastname);
//startActivity(intent);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
Intent X = new Intent();
X.setClass(getBaseContext(),NotFound.class);
startActivity(X);
}
}
retrieveStudent Activity
//this activity is a listview
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_student)
}
//Left out some code, just showing the main parts
public class StudentAsynTask extends AsyncTask<String, Void, Boolean> {
#Override
protected Boolean doInBackground(String... params) {
try {
//return result to show Activity if no records are found
if (jsonArray.length() == 0) {
Intent intent=new Intent();
setResult(2,intent);
finish();
} else {//Show list if records are found
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jRealObject = jsonArray.getJSONObject(i);
Student student = new Student ();
student.setFirstname(jRealObject.getString("f_name"));
student.setLastname(jRealObject.getString("l_name"));
student.setImage(jRealObject.getString("image"));
studentList.add(student);
}
}
In startStudentQuery, you should call startActivityForResult only, but now you have called retrieveStudentData twice.

PayPal Integration with Android

I have seen some related questions but none focusing on the specific problem I have:
I'm using the PayPal MPL Library.
I build my PayPalPayment object, then create the activity for the checkout to occur. That runs fine. My problem is, on the ResultDelegate I need to call a function from my activity, that occurs after the payment and makes some changes (such as storing SharedPreferences, etc.).
So something like this:
public class ResultDelegate implements PayPalResultDelegate, Serializable {
public void onPaymentSucceeded(String payKey, String paymentStatus) {
System.out.println("SUCCESS, You have successfully completed your transaction.");
System.out.println("PayKey: "+payKey);
System.out.println("PayStatus: "+paymentStatus);
callMyCustomAfterPaymentFunction();
}
...
}
Now the thing is, I tried to create a constructor for ResultDelegate that accepts my activity. My existing code is:
//On the activity class
public class MainMenuActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
...
Button buy = (Button) findViewByID(R.id.buy_button);
buy.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
new PurchaseTask(activity).execute();
}
}
}
}
public class PurchaseTask extends AsyncTask <String, Void, String> {
protected String doInBackground()
{
...
PayPal pp = PayPal.getInstance();
CheckoutButton cb = pp.getCheckoutButton(...);
cb.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
ResultDelegate delegate = new ResultDelegate(myActivity);
Intent checkout = PayPal.getInstance().checkout(paument, activity, delegate);
activity.StartActivity(checkoutIntent);
}
}
}
}
//On the ResultDelegate class
public class ResultDelegate implements PayPalResultDelegate, Serializable {
private Activity myActivity;
public void onPaymentSucceeded(String payKey, String paymentStatus) {
System.out.println("SUCCESS, You have successfully completed your transaction.");
System.out.println("PayKey: "+payKey);
System.out.println("PayStatus: "+paymentStatus);
myActivity.performAfterPaymentOperations();
}
...
}
So the goal is to call the activity function from the ResultDelegate. Or even simpler, just to be able to store some SharedPreference changes when the ResultDelegate onPaymentSucceeded() fires.
But I get a NotSerializableException mentioning that the my MyActivity field is not Serializable.
So, then I added the transient identifier to my activity field inside the ResultDelegate, but now I get a NullPointerException.
Paypal Mobile Chekout guide
Implementation provided on paypal website is different from yours. They are doing startActivityForResult() to start PaypalActivity. and when in onActivityResult() method they are checking statusCode to check transaction status and act accordingly.
Follow that document for your implementation.
Here in your code, I donot find a point for using AsyncTask. Your ResultDelegate is Serializable where as Activity is not thats why it is throwing NotSerializableException.
Edit:
As you are developing for Google Android platform, then why not to use Google Checkout In-App?
Edit:
This method will be called when your PaypalActivity will finish. That activity will pass resultCode to this onActivityResult method.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case Activity.RESULT_OK:
// The payment succeeded
String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
// Tell the user their payment succeeded
break;
case Activity.RESULT_CANCELED:
// The payment was canceled
// Tell the user their payment was canceled
break;
case PayPalActivity.RESULT_FAILURE:
// The payment failed -- we get the error from the EXTRA_ERROR_ID
// and EXTRA_ERROR_MESSAGE
String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
String errorMessage = data
.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
// Tell the user their payment was failed.
}
}
regards,
Aqif Hamid
You can create you custom listener something like this :
Create a custom listener :
OnDoubleTap mListener;
// Double tap custome listenre to edit photoes
public interface OnDoubleTap {
public void onEvent(Uri imgPath, int mPos);
}
public void setDoubleTapListener(OnDoubleTap eventListener) {
mListener = eventListener;
}
Now call this wherever you want like this :
mListener.onEvent(Uri, 1));
Now whenever you call this listener this will fire in your activity where you use this listener like this :
myCanvas.setDoubleTapListener(new OnDoubleTap() {
#Override
public void onEvent(Uri imgPath, int Pos) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "LISTENER WORKING !!!", Toast.LENGTH_SHORT).show();
}
});
Where myCanvas is object of class where you create you listener.
Try this solution:
PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(price),getResources().getString(R.string.curruncy_code), getResources().getString(R.string.app_name),
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(CreateEventStep4.this, PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_PAYPAL_PAYMENT);
PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.e("paymentExample", confirm.toJSONObject().toString());
JSONObject jsonObj=new JSONObject(confirm.toJSONObject().toString());
String paymentId=jsonObj.getJSONObject("response").getString("id");
System.out.println("payment id:-=="+paymentId);
screenShotFile = takeScreenshot(frmTemplate);
uploadImage(myEvent.getEvent_id(),screenShotFile);
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}

Categories

Resources