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.
Related
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...
Why doesn't AppCompatActivity's getFragmentManager() return a support version of the fragment manager? Instead you have to both make your Activity an AppCompatActivity AND specifically call getSupportFragmentManager(). The same is true for a dozen other methods. It bugs me that you have to magically know which methods you can call as themselves, and which you have to rebrand as the "support" version. Is there even any use case for wanting to call the "normal" version instead of the "support" version, when you're still supporting lower versions of Android? It'd make a lot more sense to me if AppCompatActivity functioned indistinguishably from the later API versions of Activity - after all, that's what it's SUPPOSED to do, isn't it? Provide the same functionality as the later API versions of Activity?
Is there some kind of design principle or hidden Java limitation that prevents them from doing so?
Why doesn't AppCompatActivity's getFragmentManager() return a support version of the fragment manager?
Because it can't.
Activity, on API Level 11+, defines getFragmentManager() as returning an android.app.FragmentManager. The only way that AppCompatActivity could override it is if the overridden method also returns an android.app.FragmentManager. That's not possible. AppCompatActivity is designed to work back to API Level 7, where there is no android.app.FragmentManager to return. Hence, we have getSupportFragmentManager(), returning an android.support.v4.app.FragmentManager.
Note that AppCompactActivity really gets its fragments from FragmentActivity, which is designed to work back to API Level 4, two years before API Level 11 and native fragments were implemented.
I recently updated eclipse, SDK and my problems are
By default it takes fragment_main.xml
Extends default activity to ActionBarActivity
By default it is adding the library appcompat 7
I dont have any issues with adding the library, but when i am trying to run then it is not executing in the device(2.3 android). I noticed it and changed to Activity.
If i extends it to Activity and run it and again for the next time the same problem arises while creating new Activity like...
Creating 2 xml files with extending ActionBarActiity.
I just want the new class to extend Activity but not ActionBarActivity where my target device is 2.3 and above without downgrading it
ActionBarActivity extends FragmentActivity and provides a nice bar(hehe) above your layout. Your editor is doing it correctly. ActionBarActivity was added in newer version of the sdk and it was added in the support library for older versions using appcompact v7(api 8). So most probably your development enviroment is correct. My guess is you need to check your imports and import ActionBarActivity from the support package.
It comes in SDK v4.4 . It is a plus point for developers because developers don't need to manually add the ActionBarActivity into their project. There are two solutions for your problem.
Downgrade your SDK to 4.3 or lower.
or
Remove ActionBarActivity and extends Activity in your java file.
Both will work for you.
I have created tabs using intents.MainPage.java is my class that extends TabActivity and other 5 activites that extends Activity which i have added as tabs. I need to get Strings from the 5 activities which i have added as tabs once a click is performed in the MainPage.java class
From the documentation:
The TabActivity class is deprecated. 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.
Here are some Fragment tutorials to help you get started.
Also, check out FragmentTabs.java on the developer site.
I am playing around the fragments tutorial found here on section 21. It works fine when my build target is android 4.0 but when i try to change it to make it work with android 2.2, i get Error inflating class fragment on the line setContentView(R.layout.main); on MainActivity class.
I already added the support package, changed classes ListFragment and DetailFragment to extend android.support.v4.app.ListFragment and android.support.v4.app.Fragment respectively. There are no compile errors.
Did I miss something? TIA!
--EDIT---
Made it work. I changed DetailActivity.java and MainActivity.java to extend FragmentActivity instead of just Activity. Everything's okay now. But still don't know why I have to do that.
Changed DetailActivity.java and MainActivity.java to extend FragmentActivity instead of just Activity.
Using the v4 Library APIs