startActivityForResult doesn't seem to call onActivityResult - android

When The user Click on the button it want to call the dialog- that dialog contain list of product in ListView.After user slect the product it should come to previuous activity.
I have done using startActivityForResult ().
There aresome issue.My calling activity is in normal tab activity that normal tab activty in Tab Activity Group.
Actualy i want to do in drrop down(Spinner).In my scanerio i couldn't get context.It awalys give Android Spinner Error : android.view.WindowManager$BadTokenException: Unable to add window
So I have change to my design like this: When User click buttion it load the list of product in ListView.After pick the product, it come back to previous activity.
This is my previous question : link
Here calling activity:
//Click Product button
l_prod.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent showContent = new Intent(LineDiscountActivity.this,ListProductActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Activity", "LineDiscountActivity");
bundle.putString("RetailerName", retailerName);
bundle.putString("RetailerCode", retailerCode);
showContent.putExtra("discountProduct", discountList);
showContent.putExtras(bundle);
getParent().startActivityForResult(showContent, 5);
}
});
And my receiver activity :
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String book = o.toString();
Intent i = new Intent();
Bundle bundle = new Bundle();
bundle.putString("Activity", "ListProductActivity");
bundle.putString("RetailerName", retailerName);
bundle.putString("RetailerCode", retailerCode);
bundle.putString("seletcedProductCode", products.get(position).getProductCode());
bundle.putString("seletcedProductName", products.get(position).getDescription());
bundle.putDouble("seletcedProductQty", products.get(position).getAvailableQuantity());
i.putExtra("discountProduct", discountList);
i.putExtras(bundle);
if (getParent() == null) {
setResult(Activity.RESULT_OK, i);
} else {
getParent().setResult(Activity.RESULT_OK, i);
}
ListProductActivity.this.finish();
}
And calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
Log.i("-requestCode from LineDisocunt--" ,"" + requestCode);
}
I have written this code(onActivityResult) in calling activity & Tab main Activty also.
I didn't go anywhere..
onActivityResult mehtod.But it didn't go it.
What is wrong in my code.
Please let me know if anybody know this...
Thanks in advance

I have same issue when I was using startActivityForResult() with activity group.
your activity result will go to your activity group.You will not get activity result in your first activity
So you can solve this issue by taking one public static object in your first activity and when you call second activity you have to assign your first activity object from second activity.and then finish second activity so that your first activity will resume and you can update your ui by overriding onResume() method in first activity.You have to check validation weather your object is assigned or not.
For example
You have one static object product in your first activity
First Activity
public static Product product;
start second activity
startactivity(this, SecondActivity.class);
don't finish First Activity
You have to override onResume() method and then you can use product object which is assigned by second activity
second activity
FirstActivity.product.setName(name);
FirstActivity.product.setPrice(price);
After assign the product object you have to finish second activity
like
finish()
EDIT
I got the solution for your issue of badTokenException
Here is the solution
CLICK HERE

Related

How to choose which fragment to open from specific activity?

My mainActivity contains 5 fragments which you can switch through with tabs. The main tab shown on start is the third one (the one in the middle). I have a button in fragment #1 which opens activityTwo. What should I put in the onBackPressed method in activityTwo in order for it to bring me back to mainActivity with selected tab #1. I currently have this, but it opens mainActivity and shows the main tab (#3)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
ActivityTwo.this.finish();
}
How can I make it show tab #1 instead of tab #3?
Your first activity should still be on the activity stack, so you should not call startActivity again. Instead, you can use setResult to pass a value back to your first activity:
// Inside your second activity
#Override
public void onBackPressed() {
setResult(RESULT_OK);
finish();
}
Then, from your first activity, replace startActivity with startActivityForResult, with a request code (any integer you want, as long as it's unique):
startActivityForResult(intent, YOUR_REQUEST_CODE);
Then override onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Code to select tab 1 here
// mTabLayout.getTabAt(0).select();
}
}
}
i think you are using view pager , so while you return from activity 2, use viewPager.setCurrentItem(page);
where page will be your tab no, (in this case it will be 2, since it starts with 0). Hope this helps, if not then please clarify a bit more.

How check if my previous activity exist?

In my app, there are 3 activities and their calling sequence is like this...
Splash activity--->Dashboard Activity--->List Activity
If it goes like this then If I press back button on List activity then it will follow the reverse sequence of above.
There is one requirement where user can navigate to List Activity directly from Splash (Skipping Dashboard Activity) Now when user will hit back button on List Activity then I wan't to show Dashboard Activity which is not there in the Activity Stack.
So please help me with the best approach.
Pass a boolean through the intent for going to List Activity from either of the others. Using onBackPressed check if the boolean is true or false for skipping Dashboard Activity.
Then if true put new intent for loading dashboard activity and finish(); on list activity.
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can directly go to splash from list Activity while going to
ListActivity from Dashboard Activity call finish()
Intent i = new Intent(DashboardActivity.this,ListActivity);
startActivtiy(i);
finish();
Start your inner activities with startAcvitiyForResult
Intent i = new Intent(this, Activity_2.class);
startActivityForResult(i, 1);
and in your inner activity
#Override
public void onBackPressed ()
{
finish();
}
You can also do stuff in your outer activity as you like after your inner activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == 1){
Log.i("TEST", "RESULT_OK");
}
else {
return;
}
}
}

send data to fragment from another activity in android

I have an activity which is a container for several fragments. One of the fragments starts another activity and from the second activity I want to send some data to one of the fragments. How can I do that? Basically the first activity stays beyond the second one and one of the EditViews will be updated with a new value when the second activity closes. I could've used an intent but how can I send it if the activity is already started? Thank you.
You would need to start your second activity using startActivityForResult(). In your second activity before you finish it, you need to add the data to a bundle pass this to an intent and then set the result to the intent.
Bundle bundle = new Bundle();
bundle.putString("myData", "myValue");
Intent intent = new Intent();
intent.putExtra(bundle);
setResult(intent, 0);
finish();
And then in activity 1 there should be an onactivityresult method which retrieves the value from the intent and sets it where you want in your fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getData();
string value = bundle.getString("myData");
}
I'm not sure if I have it exactly right as remembering it at the top of my head but should be enough to get you started I think.
If you want to pass data back to its containing activity from your fragment, can do it by declaring an interface handler and through that interface pass the data. And ensure your containing activity implements those interfaces.
For example: In your fragment, declare this interface as follows :
public interface CallBackInterface {
public void onCallBack(String param);
}
//Declare this interface in your fragment
CallBackInterface callBk;
#Override
public void onAttach(Activity a) {
super.onAttach(a);
callBk= (CallBackInterface ) a;
}
Within your fragment, when you need to handle the passing of data, just call it on the "callBk " object:
public void callBack(String param) {
callBk.onCallBack(param);
}
Finally, in your containing activity which implements CallBackInterface ...
#Override
public void onCallBack(String param) {
Log.d("TAG","hi " + param);
}

restart activity in android

I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.
Start activity B with startActivityForResult() and use method onActivityResult() to restart or process the new data
For example, to start Activity B:
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
Then somewhere in your Activity A class:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
// do processing here
}
}
The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.
To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:
if(requestCode == 0){
finish();
startActivity(starterintent);
}
And in the onCreate of Activity A, add starterintent = getIntent();
Just remember to initiate the variable with Intent starterintent; somewhere before your onCreate is called.
e.g.
public class ActivityA extends ListActivity {
Intent starterintent;
public void onCreate(Bundle b){
starterintent = getIntent();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
finish();
startActivity(starterintent);
}
}
private void startActivityB(){
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
}
}
Then just call startActivityB() from a button click or whatever
YES you are right. Write code in onResume.
When you updated date just call notifyDataSetChanged(); for your ListView adapter
Hope, it help you!
You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back:
#Override
public void onResume(){
super.onResume();
list.clear();
list.addAll(getBooks());
adapter.notifyDataSetChanged();
}

Switching between activity?

In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is hidden..
Now my question is
I want to bring back the first activity (i dont want to create a new instance of the first activity but to bring back the already existing instance of the first activity) when a button is clicked in the second activity ...
thanks :)
so simple. integrate the below code in your second activity
Button b = (Button)findViewById(yourbuttonid here);
b.setOnClickListener(new View.onClickListener(){
public void onClick(View v){
finish();
}
});
This will work
You would define the first activity with launchMode="singleInstance", then you would start the activity as usual.
Depending on the usage of your second activity, you could also use startActivityForResult() when you start your second activity...
FirstActivity.java{
private static final int SECOND_ACTIVITY = 0;
openSecondActivity(){
Intent forChildIntent= new Intent( this ,FirstActivity. class );
//data for second activity
forChildIntent.putExtra("userName", getUsrName());
this.startActivityForResult(forChildIntent, SECOND_ACTIVITY);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (resultCode) {
case RESULT_OK: //do something
default:break;
}
}
SecondActivity.java{
goBackButtonClick(){
Intent retData=new Intent();
//set data to pass back ,if required
//retData.putExtra("userName", getUsrName());
setResult(RESULT_OK, retData);
finish();//will take you to the first activity
}
}

Categories

Resources