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.
Related
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.
I'm trying to replace the ActionBar of my app with a Toolbar but I can't because I can't use setSupportActionBar(). It says "setSupportActionBar() is undefined for the type MainActivity" even if MainActivity extends ActionBarActivity.
I don't know what to do, any help? Do you need me to post my code?
EDIT:
Now the code works but my app crashes on start and it says: "Attempt to invoke virtual method getTitle()". What do I miss?
EDIT2:
Everything works I forgot I had a Fragment and because of that it gave me nullPointer
Now use :
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
setSupportActionBar(toolbar);
XML :
android.support.v7.widget.Toolbar
Extends :
public class MainActivity extends AppCompatActivity
Without code, my only recommendation, is making sure your import statement is as follows:
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
Also make sure in your XML files that you are using
<android.support.v7.widget.Toolbar
As your toolbar tag
import androidx.appcompat.app.AppCompatActivity;
import this class instead of:
import android.support.v7.widget.Toolbar;
or
import android.widget.Toolbar;
The newer version requires the android class
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
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
public class ActivityGoogleMaps extends MapActivity {// map view shows as an error
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
I try to import this:
import com.google.android.maps.MapActivity;
but when i type com. - google doesnt appear..
1- Go to Project Properties
2- From android tab, select Google API for your target platform
(for 2.2, select Google APIs-Platform 2.2 and things like that)
I think this can solve the problem
you will have to add map.jar to libs. though it is automatically added when we extends MapActivity.