Creating a Settings Activity on Android P - android

My SettingsActivity currently extends the Android Studio generated class, AppCompatPreferenceActivity which extends PreferenceActivity. Each of the preference screens in the activity are displayed using a PreferenceFragment; which, as of API level 28, is deprecated. The documentation states you should use the PreferenceFragmentCompat class from the support library as an alternative.
The issue is that PreferenceFragmentCompat extends android.support.v4.app.Fragment (instead of android.app.Fragment) which the PreferenceActivity does not support. And there is no PreferenceActivityCompat to fill the role of the of the now inconsequential PreferenceActivity.
Further confusing the issue; the new androidx.preference support library includes its own PreferenceFragment (which extends android.app.Fragment) and PreferenceFragmentCompat (which extends the new androidx.fragment.app.Fragment).
I could always recreate the functionality of the PreferenceActivity with my own classes, but why would the documentation recommend using the PreferenceFragmentCompat without a viable alternative to the PreferenceActivity? Am I missing something, or is the current state of the preference libraries not functional?
The SettingsActivity works as-is for now, but I usually like to try to get ahead of the curve, especially when something becomes deprecated.

Don't use a PreferenceActivity; a regular AppCompatActivity will serve the purpose just fine. If you want, there is a direct method of making a Settings Activity in your app provided by Android Studio:

Related

Getting ViewModel for SettingsActivity (MVVM, Android P)

If you choose template with Settings creating a project in Androdi Studio 3.4, you will see example app with SettingsActivity extending AppCompatPreferenceActivity extending PreferenceActivity extending ListActivity extending android.app.Activity, but not the FragmentActivity which is necessary for creating a ViewModel for SettingsActivityby means of
SettingsViewModel viewModel
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this, viewModelFactory).get(SettingsViewModel.class);
}
because of ViewModelProviders.of() may accept only the FragmentActivity as a first argument.
Is it possible to create a ViewModel for AppCompatPreferenceActivity or it is the next reincarnation of hell with preferences from Google?!
This template has been completely redone in Android Studio 3.5 to match the Settings documentation:
The recommended way to integrate user configurable settings into your application is to use the AndroidX Preference Library. This library manages the user interface and interacts with storage so that you define only the individual settings that the user can configure. The library comes with a Material theme that provides a consistent user experience across devices and OS versions.
The AndroidX Preferences Library does not require you to use PreferenceActivity at all - you'll note that it uses AppCompatActivity directly, putting preferences into a PreferenceFragmentCompat. As AppCompatActivity extends FragmentActivity, you will be able to use ViewModel and other AndroidX APIs without issue.

How to cast AppCompatActivity to Activity

I am having an activity that extends appcompatactivity I want to use the methods of activity like getwindow(), setRequestedOrientation() and finish() etc. but not able to do so... It says cannot resolve method. so, is there any way cast AppCompatActivity to Activity.
I am using Android Studio 3.0 Canary 4
For a minimum API level of 15, you'd want to use AppCompatActivity. So for example, your MainActivity would look like this:
public class MainActivity extends AppCompatActivity {
....
....
}
To use the AppCompatActivity, make sure you have the Google Support Library downloaded (you can check this in your Tools -> Android -> SDK manager). Then just include the gradle dependency in your app's gradle.build file:
compile 'com.android.support:appcompat-v7:22:2.0'
You can use this AppCompat as your main Activity, which can then be used to launch Fragments or other Activities (this depends on what kind of app you're building).
The BigNerdRanch book is a good resource, but yeah, it's outdated. Read it for general information on how Android works, but don't expect the specific classes they use to be up to date.
You can cast AppCompatActivity to Activity:
((AppCompatActivity) getActivity()).someAction...

What does android.support.v4.app.FragmentActivity extend?

The webpage with the following address states that the class FragmentActivity extends the class Activity:
https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
However, when I bring up the documentation for the android.support.v4.app.FragmentActivity class within Android Studio, it states that FragmentActivity extends android.support.v4.app.BaseFragementActivityHoneycomb.
What is the reason for the discrepancy and which is correct?
This goes up to BaseFragmentActivityDonut. These base fragments are just an implementation detail of FragmentActivity and should not concern you at all. In the library I use, FragmentActivity directly extends BaseFragmentActivityJB. In the future there might be something like BaseFragmentActivityM or some compatibility layer for even newer platforms.
For your programming just assume it extends Activity only, like the documentation states.

How to enable Ambient Mode (Always On) in a FragmentActivity on an Android Wear App?

I have an android wear app that contains a FragmentActivity:
public class MyFragmentActivity extends FragmentActivity
With 2 Fragments:
public class MyFirstFragment extends Fragment
and
public class MySecondFragment extends Fragment
I want to enable the ambient mode (always on) in this fragment activity. However, according to the documentation, the ambient mode is only available if i extend WearableActivity.
Is there a way to have both properties of the FragmentActivity & WearableActivity together in one?
or
Is there another way to enable the ambient mode in the FragmentActivity?
You should implement AmbientModeSupport.AmbientCallbackProvider instead of WearableActivity, then you can extend FragmentActivity instead.
It is the new preferred method and it still gives you all the goodies you got with WearableActivity but also lets you use Activity (or any sub classes... FragementActivity, etc.).
Official docs call out the details (and example code).
Update: Use AmbientModeSupport now instead of AmbientMode. Google recently changed the name, so the old version is deprecated.
Answer to both questions is no, sorry.
Ambient mode is enabled by calling WearableActivity.setUseAmbient(), which obviously is not available if you're not extending WearableActivity. And since Java doesn't support multiple inheritance, you can't subclass both WearableActivity and FragmentActivity at the same time - it's one or the other.
Do you really need to be using Fragments on a watch activity? If you really want to support ambient mode, you probably need to look at moving your UI out of fragments.
If you want to use Fragments on Android Wear and support Ambient mode, you need to use the AmbientModeSupport class.
Make your activity extend FragmentActivity and implement AmbientModeSupport.AmbientCallbackProvider and you're all set!
Details and examples are here: https://developer.android.com/training/wearables/apps/always-on#ambient-mode-class

Why does MainActivity in Android extend different activity classes at different times?

Could you please help me with this question?
When I create a new project I see MainActivity extending the Activity class or AppCompatActivity. Why does this happens? What is the reason behind changing the default settings of android project everytime? What are the other classes that MainActivity can extend? I would appreciate any help.
Actvity has support for system ActionBar which was introduced in Android 3.0 (API level 11).
AppCompatActivity has interface to work with ActionBar from support library and can be used with API Level 7).
If you want to support some features like actionbar, to lower API level of android so use AppcompateActivity.For example, if you extend youractivity with Activity class then you can not use actionbar in api level 7 because actionbar feature added in api level 11.
AppCompatActivity provide backward compatibility for new features
I see MainActivity extending the Activity class or AppCompatActivity.
Well it depends on you what type Activity you uses in your project.when you creates a new project it asks for Activitytype. like FragmentActivity etc.

Categories

Resources