Return result from activity inside TabActivity - android

I have a TabActivity with two Activities. One ListView for the inbox and another for the outbox.
When I click on an item(a thread) on the ListActivityThreads, I get the messages of that thread, in another ListActivityThreadDetail. When I am on that activity and answer a message I want to modify that single thread and passing it back to the ListActivityThreads.
Following the normal way to set results back to the previous activity I do this:
I use startActivityForResult in ListActivityThreads and in ListActivityThreadDetail I set a result for the intent. But I cant get any data on onActivityResult in ListActivityThreads.
I have tried different ways to start activities and passing values on a TabActivity. But no luck so far...
I cant use a BroadcastReceiver as my class already extends ListActivity.
Also I have been trying this example using TabgroupActivity and startChildActivity. But cant receive anything using the method onActivityResult, neither implementing finishFromChildActivity on my parent acitivty.
I am really stuck with this, any help would be really appreciate!

There is one alternative for this problem is that you can take one static variable in parent activity and you can access that static variable in child activity and in child activity you can modify that variable and so you will get modified value in parent activity

Related

Get activity that called parent activity of a Fragment

In my fragment I have a 'dismiss' button that should behave in a different way dependent on which Activity called its parent activity (say TutorialActivity).
In the TutorialActivity I am already determining which Activity called it. How to pass that data down to the fragment?
My fragments reside in a PagerAdapter and I wouldn't like to need to pass this info as a 'newInstance()' parameter every time, as it seems an overkill, taking into consideration that this parameter would be the same for each fragment in my FragmentStatePagerAdapter.
You can access the variable from Tutorial Activity by making this variable public. Suppose variable name is parent. You can access it by using instance of TutorailActivity (suppose instance of Activity used in fragment is mTutorialActivity) then it should be like mTutorialActivity.parent.
But you need to pass instance of TutorialActivity as it may be used for other purposes also like fetching strings from strings.xml and other purpose. So it would be beneficial to pass Activity instance instead of variable.
What I ended up doing: I implemented said "different behavior" in the TutorialActivity itself, having moved the dismiss button up from the fragment. Then a simple onClickListener and a switch statement inside of it.

Get activity instance from called activity

I have an activity containing some fragments. One of those fragments calls another activity. In this new activity I need to have an instance of the first activity. GetParent() returns null so I don't know how I can acomplish this...
MainActivity --contains--> Fragment1 --startActivity()--> SecondaryActivity
Is there some way to get the calling activity on the SecondaryActivity?
I don't think there is a good way of doing so.
It's really a bad practise to handle activity references like that, since android wouldn't be able to gc them when needed (orientation changes, lack of memory).
The best way is to pass all the data you need with Intent extras (intent that you use to start activity), and, if you need SecondActivity to return something, use Fragment1.startActivityForResult() for starting activity, and when done, use SecondActivity.setResult() to return desired result, you will need to override onActivityResult() to get the result (there are plenty tutorials about this).
If you absolutely need to hold references to something, you can use your own instance of Application class (don't forget to declare it in the manifest) to hold data for you while application is running.

Completing fragment transaction when a task is done

It might be an easy question but I have been working on it for hours and couldn't solve it. Sorry if it was asked before.
In my activity I have 2 Fragments. In one fragment I am getting JSON from server and putting in ArrayList. After putting in ArrayList I have to add another fragment passing first value in ArrayList.
Here is the problem, I want to add Fragment when my ArrayList is completed, after it got all values from server. I am making service call in my onActivityCreated() method, server call is happening in another class and I am getting Bundle in a method called onRequestFinished() and I am putting JSON to ArrayList in this Overridden method.
P.S. I tried to put AsyncTask but I couldn't make it since I have to wait the response from onRequestFinished() method.
How can I handle it and complete Fragment Transaction after filling my ArrayList?
Thanks
I suggest using startActivityForResult in the first fragment. When the parent activity receives the onActivityResult it can then start the second fragment. There are plenty of examples of how to do this on SO and elsewhere.
Even if you do not want to start an activity, you can use the same mechanism to communicate back to the activity when your first fragment has completed e.g.:
Is there a method that works like start fragment for result?
Alternatively you could of course just implement a callback mechanism from your first fragment - but personally, I like the onActivityResult approach.

Passing pointer to Activity to next Activity

I'm very new to Android programming (and Java for that matter) coming from an iOS background.
What I am trying to do, is pass a pointer to a Fragment from one Activity to another.
Basically, I have a starting activity called BeginActivity that handles a couple of Fragments for login and register screens. Once logged in, I load up the main activity of the app called TabsFragmentActivity using this code:
public void loggedIn() {
Intent intent = new Intent(this, TabsFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
finish();
}
I'm using FLAG_ACTIVITY_CLEAR_TOP as I dont want the user to go back without actually logging out first.
Now the problem:
In BeginActivity I have a pointer to a fragment that holds the users data. I am using it like a singleton, that the first few view fragments can access from BeginActivity.
I need to pass this same object to the new TabsFragmentActivity before I call finish() on it.
How do I do this?
I know I can use putExtra() but I believe that is just for strings etc.. and not other Fragments.
Is there a way in the newly created TabsFragmentActivity that I can reference the BeginActivity to 'grab' the pointer?
Thanks
First of all, you should be sure about Fragments and Activity life cycle.
Fragments are designed to be reusable UI complex components. They look like activity, but you can reuse. So,you can have as many activities you need containing the same fragments, but not the same instances of these fragments.
If you just want to pass you user data for another activity you must use Bundle and putExtra(). Depending of the user data type can be necessary implements Serializable or Parcelable Interfaces, as #gheese said.
If you want to use the same UI appearence of your fragment on two or more activities, besides use Bundle and putExtra. Each activity that you want this behavior must contains a field whose is a Fragment and in the moment of starting this fragment you can use getActivity().getIntent().getExtra to get the user information and populate your fragment.
Basically you need to be able to pass your class via the intent, look at Serializable / Parcelable interfaces
This question has the answer you require
How to pass an object from one activity to another on Android

Android access activity1 from activity2

Hi i am new to Android application development.In my Application i have two activity Activity1 and Activity2.From activity1 i call Activity2 as Intent.I want to access activity1 from this Activity(activity2) without going to first activity is there any posible way?Pls guide me
The only thing which make sence is passing data from Activity 1 to Activity 2. To do it just pass some data through the intent:
intent.putExtra("key", "Your data here");
in second activity:
String data = getIntent().getExtra("key");
If this is not the case, then your task is wrong somewhere. When activity gone background, there is no sence to interact with it.
No, there isn't. Activity1 might even have been shut down.
If you want to pass DATA between the two activities, that's of course doable. Either by passing data on with the intent, by using http://developer.android.com/reference/android/content/SharedPreferences.html or any other storage you can access from both activities.
If from your second activity wants to change something on the previous one, then, instead of using
startActivity(...);
you should use
startActivityForResult(...);
Maybe this link can help.
So you have the scenario where you start activity B from activity A and you want to change some parameters when activity B is done ( your changes can't be propagated in real time because you can't be sure what is the state of activity A ). So the best way to implement this is to use activity result - for more info about it check Android: Capturing the return of an activity

Categories

Resources