How to use Android activity fragment from other packages? - android

I have just begun development on Android ICS.
I have a requirement to reuse some activity fragments inside my application.
For example, when I want to write a 'setup wizard' application, I need to reuse the WIFI setting activity fragment in my own APK.
But it seems that activity fragment can only be used within the same package(APK).
Does any one know how to deal with this?
Thanks a lot.
-Robin

But it seems that activity fragment can only be used within the same package(APK)
Correct.
I need to reuse the WIFI setting activity fragment in my own APK
I do not know if your app can have all the permissions necessary to do all the things the Setting app's WiFi screen does. That being said, you are welcome to browse the source code for Settings and pull out what you want.

Related

Always use fragments in Android

I'm just getting to know Fragments in Android.
When you create a new blank project, by default a Fragment is included as well, although it's not really used. My impression is that Google wants you to use Fragments all the time no matter how simple the app is. Would that be a true assessment or can you think of any reasons not to use Fragments?
What I also find strange is that the documentation indicates that Fragments can also be used for stuff that is not UI related. Can you give me an example of an app that would use a Fragment but doesn't provide any UI?
Google introduced fragments when they lunched Honeycomb (3.0).
As you may know, Honeycomb was the first Android version to support tablets out-of-the-box and they use fragments to better arrange UI layouts on the screen.
With Fragments you can utilize screen property way better then with activities. One activity can run and "command" many different fragments that share same screen real-estate and the fragments can be swapped on the fly.
So yes, Google WANTS you to use fragments and it is the right way to write most scale-able applications.
As for the second question:
Fragments can persist across configuration changes - like screen orientation changes for instance. Activities gets killed and recreated when you change screen orientation and any work they might do will have to be recreated again.
If you use fragments right, then when you change screen orientation the activity might get killed but the fragment can persist its state and then re-attach itself to the newly created activity and continue where it left.
Basically, if you have an AsyncTask running from an activity and the activity gets killed because of orientation change (for example), you AsyncTask is useless now. But if you hold the AsyncTask through a fragment then it will continue because the fragment isn't destroyed.
Hope this helps
It all depends on what you're developing and how. I don't think EVERY app needs to have fragments, but they are in many cases easier to work with as they can be swapped on the fly, managed by a single activity, etc.
For instance, imagine your app has some background task running, and you want it to keep running while the user is still "Free to roam" around the app. Running that task in the activity and having the UI in fragments would be a very simple way to do this. The activity can also manage and send messages and data to its "children" fragments at any point, including communication between the fragments themselves.
As for fragments with no UI at all, I don't recall coming across something like this, but you can definitely implement background tasks and other methods that are not directly related to the UI in a fragment. Again, it all depends what you're developing and how. There's really no "right or wrong" here...

Is fragment only useful when the application is designed to run on both phone and tablet?

I am developing a forum reader, which I expect that it will be run on phone only, not tablet.
The application originally have 3 views, and each is assigned with an Activity.
For example, if the user select a board on "Board selection screen", an Activity will start, and show the thread list of the selected board to the user.
Recently I have learned how to use fragment in an Android application. therefore I decided to convert my application from an Activity based apps to a Fragment based apps.
Although the apps works as fine as usual after the change, I encountered a problem in saving the state of the fragment. I failed to find the event to let me save the state of fragment (When I am switching from A fragment to B fragment). The most likely event is onSaveInstanceState(Bundle), but according to the API guide, it will only be called when an activity is about to be stopped, and it is not my case.
Actually I have tried to save the state in getArgument() in onDestroyView(). Nevertheless, according to the API guide, the values in getArgument() should only be used for instancing a fragment. Although everything work normal when I am saving the state in getArgument(), I believe that I am doing it wrong.
I have tried to do some googling about Fragment. I found that most of the artist suggest that Fragment is useful when an application is expected to run on both phone and tablet. And I don't find any artist that is talking about how to do screen switching between few Fragment within an Activity. I started to doubt that Fragment are only useful for supporting different device, but not for screen switching.
Sorry for the above junks. My questions are, does Fragment is not very useful when I am developing an application which for phone only? Should I change my application back to an Activity-based apps if I don't expect this to run on tablet?
One more thing Fragments are good at is they are good for Dialogs. Fragment-based dialogs have none of the problems you usually have with showDialog() when rotating the phone
No, you should stick with Fragments. They're incredibly versatile and will make your life easier down the road if you decide to rearrange things. As far as saving state, you just need to override onSaveInstanceState() in your Fragment (not the Activity). This will absolutely be called when switching between Fragments -- I use this all the time.
If we're talking about "just work" you can just go ahead and use fragments or just normal activities and layouts. However, what fragments provide, apart of scalability when developing for other devices like tablets, is that it's giving you more code separation, which is so, so great for the maintenance of the app.

Cut phone screen on two parts that do simultaneously two things

Can i create a program which will cut phone screen on two parts.
Those two screen will do simultaneously two things, for example to watch a clip and to read and write sms.
I think that:
1) i need to create a home luncher application first.
How can I create a custom home-screen replacement application for Android?
2) Then i can start two fragment in my activity.
http://developer.android.com/guide/components/fragments.html
3) Finaly i open app1 in first fragment and app 2 in second fragment. Can i do that, open an application in a fragment?
Any other ideas will be usefull.
I think you need a modified version of the Android ROM to be able to do this. With the standard ROM you can only have 1 application running in foreground at any given time.
Samsung has done something similar on the Galaxy Note by allowing you to watch video while doing other things.
If you don't want to change the ROM you could implement this on your own apps, but not on all the apps.
I'm no expert, but it seems to me that you can have one application with two fragments in it. The top fragment would display video and the bottom fragment would have the messaging. Since fragments are only API 11 and up you don't have to worry about old phones being able to handle it.
Your step 3 states that you'll open apps in your fragments and I don't think that's how they work.
You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
Read more about Fragments here:
http://developer.android.com/guide/components/fragments.html
You can customize the launcher and the homescreen and lancher, check out the links below
https://android.googlesource.com/platform/packages/apps/Launcher2.git
How can I create a custom home-screen replacement application for Android?
Compile Launcher/AOSP from Eclipse:
Android Launcher application compilation on Eclipse
Read this for your information on how to compile and run your custom launcher.
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/zI9LPeU1mbc

How do I detect orientation change for a fragment in Android?

I am in a process of developing an Android app which have a activity and many fragments to that activity. I want to check and change the orientation of only two fragments among the many.
How do I do this? I want only these two fragments to change their orientation and not all. I came to know that we have to use OrientationEventListener for this, but I have no idea how to use this.
I am working on mobile device app and with Android 2.2 and fragment support files.
I went through the following links but made no reference to fragments:
this and this.

Android Activity Tear down rebuild

Under some circumstances I need to be able to tear down all my activities that are in my application stack and recreate them all due to configuration changes. I have accomplished this by first calling finish for each activity and then recreating the stack.
To recreate, I relauch my root activity. And within its onStart I have it create my second activity. Within my second activities onStart I have it create my third Activity. This does work but the problem that I am having is that when watching the screen you see each of the three activities created and animate into the next activity. I want to have this rebuilding invisible to the user and hide these transitions. Does anyone know how to accomplish this?
Android already takes care of restarting activities when there is a configuration change.
If you are saying you want all of your activities to be restarted, even if they aren't currently visible (Android will do this lazily as the user returns them and they become visible, if the configuration is still different at that point), then no there is no simple way to do this. I can't imagine you coming up with anything that isn't going to be hideously ugly, because to get the platform to restart your activity you will need to make it visible, and then you are going to have flicker up the wazoo.
Things just aren't intended to work that way. This isn't how pretty much any other application you run on Android will operate, so if you deeply feel like it is something you need to do then it will be useful to explain why that is so we can tell you a better way to accomplish what you want. :) For example, if you have a bunch of activities whose state is fundamentally tied together to require this, consider using fragments instead (or cleaning that up).
On the other hand, if you just have some internal concept of a configuration and want to get your activity to be restarted (say for example to switch between themes), there is an API for this but it only was added in Android 3.0: http://developer.android.com/reference/android/app/Activity.html#recreate()

Categories

Resources