Problem in opening Sub Activity of ActivityGroup - android

I am working with ActivityGroup and I want to open an sub activity of the ActivityGroup on selection of another tab. Please anyone let me know if it is possible or not?
example - I have tab1 and I open a subActivity(say activity "A") in the same tab using ActivityGroup and now when I click on tab2 I should open the same Activity "A" with all its contents.
Thanks in Advance.

I really, strongly urge you to stop using ActivityGroup. This has been deprecated.
The fragment support library provides a much better way to do these things. The 3.2 version includes an example showing how to use it with the tab widget. See the "Fragment Tabs" example and the TabActivity documentation.
These can be used down to Android 1.6.
(There are also lots of demos there for other things you can do with fragments.)

This is how I thing it can be done :
open the sub-activity just as normally you do, create certain static variable to hold data between two opened activities, be it static bundle or strings, when ever you perform some action on activity ex: type some thing in edit text, save the content in those static variables, in the onResume of the activity, put those data from variables in views........
See if it works........

Try this code,
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
if not found helpful then please provide me some more details about flow and if possible then put your (xml/java) code. so i can clearly understand the problem.
Let me know it's useful for you or not by comment ?

Related

Catch Exception in Activity when Fragment Throws

So I'm sure there's an easy answer for this one, but I haven't found it online. I'm using a fragment in Android that uses Play Services, and I want to throw any exceptions from using that fragment back to the activity that hosts it so that I can take the appropriate action depending on the activity. Where would I look to figure out how to do this?
Thank you.
You can define an interface to allows communication between Fragment and Activity.
Take a look at the Documentation

Fragments, Activities, FragmentActivity

I've read many posts on stackoverflow about fragments vs activities, but I'm not sure I understand. Am posting a problem I'm working on -- hopefully some of you could help me clarify what they mean in this context.
I want to build an app with two tabs: "take photo" and "browse photo". In take photo, the user can take a photo. In browse photo, the user can browse photos already taken.
So I've made two tabs so far in MainActivity, which extends FragmentActivity and implements ActionBar.TabListener. onCreate of MainActivity creates a SectionsPagerAdapter, which extends from FragmentPagerAdapter. The main purpose of my SectionsPagerAdapter is to create new Fragments. It creates a TakePhotoFragment and a BrowsePhotosFragment.
Question: in TakePhotoFragment, should I create a new activity that takes the photo? I do know how to create an activity that allows the user to take a photo, but not sure if this is right in this case. I did read that fragments are primarily for UI reasons and sit within activities, so it seems kind of weird to create an activity in a fragment (and also I don't know how this could be done).
I guess the main problem I'm facing is that I'm really confused about how activities and fragments can be used, despite all the reading I've done about them. Perhaps someone can help elucidate in the context of what I'm trying to do?
Thanks!
Well just some clarification, a fragment is a content that be put into an activity, not an activity. You will always have one Activity which can host fragments but not the other way.
For the camera thing, it's like using any other feature, you will call an external activity or service, you can do that inside the fragment, just like voice recognition or barcode scan.
Here is a simple example you can try, and adapt it for a fragment.
http://developer.android.com/training/camera/photobasics.html
Hope it helps, good luck with your research .
First of all, you CAN'T create an Activity inside a Fragment.
If you have a code which works within an activity it should also work within a fragment. Just use the fragment's onCreateView instead of Activity's onCreate.
If you use some kind of SurfaceView for taking photos it works within any context. If you call an outer camera intent it has nothing to do with your layout, it just calls the device's photo app, so you can't put it inside your fragment.

Wrap Activity in a fragment, or an alternative solution

I have a kinda big, already published railway schedule app published on Google Play, and now I am planning to integrate bus schedules as well. My question is something like this:
At the moment, whenever the app starts, it loads the Activity, where you can search for train, lets call it A
I have already implemented a beta version of the bus search Activity, lets call it B
Eventually, I will come up with a hybrid solution, that will be able to search for trains AND buses at the same time, of course with some restrictions, lets call it C
Now, I want to implement the main screen so it will have swipeable tabs, and the tabs would be A, B, and C. I never really worked with theese kind of tabs, so my knowledge is not that deep, however, I came up with a solution, that would indeed work, but it is going to need a lot of recoding(keep in mind, my app was published more then a year ago), since I never really prepared for fragments. I am doing pretty much THIS thingie, and it works really great. However, this means that the main screen should be a FragmentActivity, which is fine, I can deal with that. But I just cannot supply A, B, and C Activity, as tabs, it will accept only fragments.
The question is: can I supplement my already coded Activity as a fragment, so I can swipe between them? Is there any wrapping mechanic I am not aware of? Any other solution, which you can show me? Just point me in the direction, I will do the rest. Or, am I f**ked, and have to recode everything from scratch? No problem, if that's the case, tho I would happily avoid it.
Thanks in advance, cheers. :)
What you can do is to wrap your fragment in an Activity. This would be the better choice if you've got multiple classes enclosed in your Activity. Otherwise, I would just go with wangyif2's suggestion instead.
In any case, should you want to do the former:
public class SampleActivity extends Activity {
...
public static class SampleFragment extends Fragment {
...
}
}
And when you finally initialize all this in an ActionBar
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.addTab(bar.newTab()
.setText("Sample")
.setTabListener(new TabListener<SampleActivity.SampleFragment>(
this, "sample", SampleActivity.SampleFragment)));
I suggest checking out the API Demo from API 14+ samples. There's a really good example of this in FragmentTabs (com.example.android.apis.app.FragmentTabs).
Thank you both for the suggestions, I did try the method wangyif2 suggested, it generates way too much overhead in the code, since I did some kinda specific things.
I am going to check out some demos, but I guess I will recode everything from scratch, that seems to be the most clean solution at this point.
Instead of your original Activity A, B, C extending SherlockActivity, simply change them to extend SherlockFragments, and supply them to the Tabs.
Obviously there will be some compilation errors that you have to fix, namely the difference between activity lifecycle and fragment lifecycle, but once you fix them, you should be good to go.

Android TabActivity pass parcelable across tabs

I'm trying to pass data from tab n to tab n+1 activities, based on TabActivity in the MainContainerActivity, using parcelables but can't succeed as I can not apply "getExtras" method on the tab n+1 intent.
I can pass my parcelable object from one tab to the next one if I start the tab n+1 activity using startActivity(nextactivity) instead of using the tab switcher with tab.setCurrentTab(tab_n+1).
How shall I manage the activity intents sothat I can succeed to pass those parcelable objects, still implementing TabActivity in the MainContainer class and Activity in the tabs ?
or is there a way to force the activity start in the manifest when using tab.setCurrentTab(tab_n+1) ?
I read dozen of threads and have been trying many misc tests but w/o success so pls detail the answer as for a real noob :-)
EDIT:
Looking to get this working from API8 onwards
As explained here :
Restarting an activity in a single tab in a TabActivity?
What you want is to destroy the activity and start it again, by passing the new Parcelable "arguments" in the intent.
The suggested method will probably work, however I strongly suggest you to consider using Fragments instead. If you want to support version prior Android 3, you will need the support package.
The examples listed in this thread: https://stackoverflow.com/questions/5710573/need-a-fragments-example should get you started on Fragments.
I guess the simple answer to my simple 3 tabs application is using a global object as described here, which also allow keeping my existing source code.

Workaround for Android bug pertaining to setRetainInstance(true)

Please check out this issue: http://code.google.com/p/android/issues/detail?id=20791
The project (https://github.com/kaciula/BugRetain) uses a CursorLoader to take 2 values from a database through a content provider and shows them on screen. The scenario is this: From activity A, go to activity B, switch once the orientation and go back to activity A. The values from db are no longer showing.
Can anyone provide a workaround for this issue? The problem doesn't appear with only CursorLoader but with any loader. As a consequence of this bug, I can't write an app with fragments that use setRetainInstance and is available in both orientations. I really need a workaround until the Android guys fix the issue. Any ideas?
Do not set your fragments as retainable if you use Loaders. If you need to store some data between configuration changes, create another retainable fragment and pass this data to him.
A similar example can be found here: FragmentRetainInstanceSupport. But this example uses extra fragment for threading purposes. In your case this extra fragment will be used as data container.

Categories

Resources