Is setRetainInstance is supported with FragmentActivity - android

I want to retain the instance of an object on configuration changes using Fragments. And I want to support older versions of android using support library. So I extended FragmentActivity like this.
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class test extends FragmentActivity {
#Override
public void onCreate(Bundle state){
super.onCreate(state);
setRetainInstance(true);
}
}
But when I do this, Eclips complains that there is no such a thing as setRetainInstance. But when I change the FragmentActivity to Fragment every thing is OK.
what am I missing?

setRetainInstance(Boolean) is a method in Fragment class. not of FragmentActivity class...
and There is a support library for Fragments in Android SDK.
see at android-sdk/extras/android/support/v4/android-support-v4.jar

Related

How to use android.support.v4.app.Fragment with Activity class?

Ok here is the issue, the native Android Fragment is deprecated.
This will be use in an Unity Android native plugin.
In the old ways you simply create a Fragment class like this:
public class UnityAndroidNativeplugin extends Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
UnityPlayer.currentActivity.
getFragmentManager().
beginTransaction().add(instance, UnityAndroidNativeplugin.LOG_TAG).commit();
}
}
That works fine but I still don't get, if this is deprecated we should use the support fragment library. Ok I will update this.
So when I change to the new version it should be something like this.
public class UnityAndroidNativeplugin extends android.support.v4.app.Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
FragmentManager fragMan = UnityPlayer.currentActivity.getSupportFragmentManager();
//This Fails
}
}
I already know that my main Activity should be a FragmentActivity.
Cannot call getSupportFragmentManager() from activity Check here.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)?
One solution is to override the Unity Main activity but that could have problems when using other plugins. Or maybe using something else than a fragment.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)
Correct.
if this is deprecated we should use the support fragment library
Or, better yet, the AndroidX one, as the Support Library one will become obsolete before long.

What is a difference between PreferenceFragment class and PreferenceActivity class and what they do?

Here I have an example of the code for showing an app preference. The first code is a class which extends PreferenceFragment and the second is a class which extends PreferenceActivity.
PreferenceScreen xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="my_nickname"
android:title="Enter your nickname"
android:summary="Here you need to enter your nickname if you want to change it">
</EditTextPreference>
<ListPreference
android:key="color_key"
android:title="Favorite color"
android:summary="What is your favorite color to change your color preference"
android:entries="#array/favorite_colors"
android:entryValues="#array/colors_numbers"
android:defaultValue="1"/>
<CheckBoxPreference
android:key="notification_key"
android:title="I want to receive a notification"
android:summary="If you check this you will receive a notification"
android:defaultValue="false"/>
</PreferenceScreen>
Extend PreferenceFragment class:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class CustomPreferenceWithFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Extend PreferenceActivity class:
import android.preference.PreferenceActivity;
import android.os.Bundle;
public class CustomActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new CustomPreferenceWithFragment())
.commit();
}
}
QUESTIONS:
What is exactly the job of the class which extends PreferencedFragment and what is the job of the class which extends PreferenceActivity?
What is a meaning of android.R.id.content?
I know that fragment has to be connected with an activity but why here fragment is not connected with Activity class(extends Activity or AppCompatActivity) instead of PreferenceActivity which is placed here?
Both offer the same methods, more or less, to edit the internal SharedPreferences of the application, loaded from a file with addPreferencesFromResource
I feel like the documentation summarizes best
If your app supports versions of Android older than 3.0 (API level 10 and lower), you must build the activity as an extension of the PreferenceActivity class.
On Android 3.0 and later, you should instead use a traditional Activity that hosts a PreferenceFragment that displays your app settings. However, you can also use PreferenceActivity to create a two-pane layout for large screens when you have multiple groups of settings.
That being said, Android has evolved far beyond 3.0 API, so it's safe to consider PreferenceActivity as deprecated. Even pre-3.0, I believe there is a support library with PreferenceFragment class.
What is a meaning of android.R.id.content
Its the root element of the screen - Android: What is android.R.id.content used for?
why here fragment is not connected with Activity class(extends Activity or AppCompatActivity) instead of PreferenceActivity
Well, PreferenceActivity does extend the Activity class, so there is no real reason to use that specific one if you only are loading a Fragment
Here I summarized all my investigation of this problem:
After reading the documentation from Settings I realized that here in my example I can replace a PreferenceActivity with regular Activity and everything works fine.
Actually, Android documentation says that I should use PreferenceFragment if I am developing for Android 3.0 (API level 11) and higher and PreferenceActivity is for lower API than 11.
Here in my case, I used PreferenceFragment as I should but I added the fragment to Activity with the class which extends PreferenceActivity although I should use a basic Activity.
Here is my new code for adding a fragment to Activity:
import android.app.Activity;
//import android.preference.PreferenceActivity;
import android.os.Bundle;
public class CustomActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new CustomPreferenceWithFragment())
.commit();
}
}
Thanks to cricket_007 for an explanation and documentation.

Fragment can be imported with FragmentActivity only, targetting sdk 15? (v4? v7? v13)

this thread says :
FragmentActivity is for use with the backport of fragments found in
the support-v4 and support-v13 libraries. The native implementation of
fragments was added in API Level 11
I am using a min sdk of level 15 and I was only able to make the import of fragment work with FragmentActivity and getSupportFragmentManager, instead of extending Activity or AppCompatActivity and using getFragmentManager :
public class MainActivity extends FragmentActivity implements TaskFragment.OnFragmentInteractionListener {
private TaskFragment mTaskFragment;
private static final String TAG_TASK_FRAGMENT = "task_fragment";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Would someone know why, and have some details about the sdk levels that should be used with v4 or v7, or v13(?)
Thanks
You should extend you activity from AppCompatActivity and use getSupportFragmentManager.
I don't know what TaskFragment is – something third party I presume.
Remember to add the Google Android support library com.android.support:appcompat to your project.

v4 getFragmentManager with Activity - Incompatible types

I have a simple activity which runs as expected.
import android.app.Activity;
import android.app.FragmentManager;
// import android.support.v4.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
FragmentManager fm = getFragmentManager(); // Activity
}
}
I then replaced
import android.app.FragmentManager;
with
import android.support.v4.app.FragmentManager;
so I could support my older devices.. However, this reports an error:
Incompatible types.
Required: android.support.v4.app.FragmentManager
Found: android.app.FragmentManager
What am I doing wrong here?
The popular solution I found is to use getSupportFragmentManager() instead, but this only works for ActionBarActivites [edit - see answers] and FragmentActivities.
cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager
The other relevant solution points to using a FragmentActivity instead, but this appears to have the same legacy problems.
The method getFragmentManager() is undefined for the type MyActivity
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
FragmentManager fm = getFragmentManager();
}
}
I'm pretty sure the solution to this will be on SE already, but it isn't easy (for me) to find. The minimal example should help other people with understanding it, too.
I'm fairly new to Android.
In your first case if you use getSupportFragmentManager() you need to extend FragmentActivity or extend ActionBarActivity (extends FragmentActivity) as FragmentActivity is the base class for support based fragments.
In your second case you need to use getSupportFragmentManager() instead of getFragmentManager().
Fragments were introduced in honeycomb. To support fragments below honeycomb you need to use fragments from the support library in which case you need to extend FragmentActivity and use getSupportFragmentManager().
use getSupportFragmentManager() instead, but this only works for ActionBarActivites.
Wrong, it should work in your FragmentActivity, that FragmentActivity is in your support package, when you are supporting older device then all your import from activity,fragment, fragment managers, etc. must have android.support.v4. to specify that you are using the support package. without using so will result to incompatible compile time error.
What am I doing wrong here?
You are combining support package with non support package which result to incompatible compile time error, as I said above.
for Kotlin guyz u dont have to change to not v4
use below to get the activity as long as its the parent
(activity as MainDrawerActivity)
or
((YourActivityName)getActivity());
For use in Java Dialog:
- import android.app.FragmentManager;
- FragmentManager fragmentManager;
For Kotlin :
- (activity as? YourActivity)?.fragmentManager

The Type TabActivity is deprecated

I trying to extends my main class from TabActivity ... I wrote the code as bellow :
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class AndroidTabAndListView extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
.
.
.
.
.
}
but I have a worning that the type TabActivity is deprecated !!!!!
Have any one any Idea about this ???
thank you in advance , Fadel
Please see the official document.
This class was deprecated in API level 13. New applications should use
Fragments instead of this class; to continue to run on older devices,
you can use the v4 support library which provides a version of the
Fragment API that is compatible down to DONUT.
See also:
"The type TabActivity is deprecated" For app tab
https://stackoverflow.com/questions/12600207/the-type-tabactivity-is-deprecated-help-to-use-in-older-device
Android: TabActivity deprecated, use Fragments?
Or if you have a more specific question, you can add more info.

Categories

Resources