Using DialogFragment - android

I want to throw a Dialog to the user of my app to confirm some action. From the developers API guides I learnt that they prefer using DialogFragments instead of dialog class.The activity which should show the dialog is a ListActivity and already has been coded.
My question is, if I need to use the DialogFragment then my activity may still extend ListActivity? Or I need to extend FragmentActivity (my app has min sdk 2.3.6) as I know android does not support multiple inheritence?
I dont want to use ListFragments
Thanks for any help.

I think you need to use the android-support-v4 library and then create a class that extends FragmentActivity but import this line android.support.v4.app.FragmentActivity; to support fragments from v2.3.6 and up. Change your ListActivity class to then extend ListFragment and invoke the class from your FragmentActivity. You can then use the DialogFragment with other fragments. This will change the whole structure of your project but it will force you to use better up to date api code.
Seeing that you don't want to use ListFragment I wouldn't go through all this trouble of converting all your classes into Fragments just to use DialogFragment you can just use normal Dialogs, they aren't a deprecated api so I wouldn't say there's anything wrong with them.
I think the reason DialogFragments are preferred is because they want you to use Fragments when developing your app, due to versatility for Tablet apps ect.
Hope this helps

Related

How to extend multiple Android activities

Say someone wants an Activity which both has an action bar and a preference, the first idea in mind is probably
public class MyActivity extends ActionBarActivity, PreferenceActivity
But Java doesn't allow this. I know API 11+ Activities has actionbar builtin. It's just an example of wondering how to use multiple features from multiple base classes.
EDIT: Based on the feedback it seems we have to hack in this case. IMHO it could be as simple as putting all activity utilities as fields in class Activity and implement getter/setter to use those utilities. Well, in reality, it isn't.
No you cannot extend from two classes in Java. Typically in Android to add the ActionBar to the older PreferenceActivity there are a couple of hacks you can do or libraries that also do the same thing. However, recently with the new AppCompat library they introduced the Toolbar widget which can be used to add an Actionbar to your PreferenceActivity in this case. For more information, checkout this post I recently wrote on how to add a Toolbar to your legacy SettingsActivity.
simple solution:
Firstly you can't extend multiple classes..java does not support multiple inheritance see here
Secondly using action bar sherlock library here, this gives you action bar functionality without extending the actionbaractivity plus its backwards compatiable.
Or...you can implement a custom action bar go here
As mentioned in the other answers, Java doesn't allow multiple inheritance.
If you want an ActionBar as well as something such as Preference functionality, consider using a PreferenceFragment
It's not quite the same as multiple inheritance but Fragments allow adding extra functionality to Activities.
You can create a subclass of the PreferenceActivity, called AppCompatPreferenceActivity (or whatever you would like), to use an AppCompatDelegate to provide the SupportActionBar functionality. You can then subclass the new AppCompatPreferenceActivity for your MyActivity class like so:
public class MyActivity extends AppCompatPreferenceActivity
For how to do this, check out the AppCompatPreferenceActivity sample code from the Chromium project.

Using support library v4 fragments in custom DialogPreference

I'm trying to implement custom DialogPreference, whose layout will use fragments. An app should support API 10 (which does not natively support fragments).
In order to work with fragments I need to get an instance of the android.support.v4.FragmentManager (normally usually being received from support FragmentActivity using getSupportFragmentManager).
I have no idea how to get android.support.v4.FragmentManager within DialogPreference. Any clue is appreciated.
If you are calling the dialog from an android.support.v4.app.FragmentActivity you should be able to do a type cast in your DialogPreference class, to get the Support-FragmentManager:
(android.support.v4.app.FragmentActivity)getContext()).getSupportFragmentManager()
... if that's not possible, another solution would be to launch an activity that looks like a dialog, and use your fragments there. How to open an activity from a PreferenceScreen?

What should I use, the 'normal' or the support library?

I often have the problem, that I don't know which library I should use.
Here is an example: I want to make a App which uses Fragments
So do I use the android.support.v4.app.Fragment or the android.app.Fragment
Another example: android.view.View.OnClickListener or android.content.DialogInterface.OnClickListener
Where to find out which is the one I should use? (Note: This is just one example of making a desicion)
Fragments were introduced in api level 11. So if you want to use Fragments below api level 11 you need to use Fragments from Support Library. Your Activity need's to extend FragmentActivity. So you will import android.support.v4.app.Fragment
If you are supporting apps api level 11 and above. You can extend Activity and use import android.app.Fragment
android.view.View.OnClickListener is used for views such as button , textview click listener. If you are using dialog then you use android.content.DialogInterface.OnClickListener
View.OnClickListener
Interface definition for a callback to be invoked when a view is clicked.
DialogInterface.OnClickListener
Interface used to allow the creator of a dialog to run some code when an item on the dialog is clicked..
About the fragments, it depends on your app target. If your app isn't going to support android versions lower than 4.0, then you should use android.app.Fragment because those fragments have better functions and flexibility. But if you'll support lower android versions then you should use android.support.v4.app.Fragment.
About the ClickListener, if you are setting the listener to a regular view (Button, Image, TextView, etc...) you should use android.view.View.OnClickListener. If your are setting the listener to an item inside a dialog, the best option would be android.content.DialogInterface.OnClickListener

ActionBarCompat and MapActivity

I've successfully converted most of my app to use ActionBarCompat by extending ActionBarActivity on most activities, however... I have a MapActivity class and need this to work here as well.
How would I go about creating a new class based on ActionBarActivity and having it extend MapActivity and would that even work?
The sample that Google provides in the ../samples/android-17/ActionBarCompat directory has a lot of these java files (ActionBarActivity.java, etc.) but also tons of required layouts, styles, drawables, etc., etc. and I wasn't sure if adding all of those files to my project is the only way to get this one map screen to work. Seems like there has to be a better way right?
You can't create an activity that inherits from both. You'll have to create a MapFragment.

existing activity into fragments

I have created a tab fragments in android 2.2 , with a android compatibility support library , now in my application i have few activities some of them are extends Activity class and some of them extends ListActivity.
so how can i convert the existing Activity or ListActivity into Fragments so that i can take the advantage of Fragment features ?
As to create a fragment , one has to extends Fragment class but if an activity is deriving ListActivity then what to do to convert it in a fragment?
You need to review the Fragment documentation and samples on the Android Developers website. This will explain what a Fragment is able to do, and what you should be doing inside of your fragment.
In essence, its a very simple transition over to using Fragments once you have looked over the examples. You will need an Activity to contain the Fragments still.
To make this a lot simpler, I would advise you look into the ActionBarSherlock library, which will allow you to use the ActionBar and SupportLibrary back to 2.1.
To get you started, you will want to use the Fragment and ListFragment classes, which will be very similar to a standard activity, but the life cycles are a little different with a few naming changes.
You could try deriving it from ListFragment

Categories

Resources