How to cast AppCompatActivity to Activity - android

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...

Related

Creating a Settings Activity on Android P

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:

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.

How to extend ActionBarActivity and YoutubeBaseActivity?

Is there a way I can extend both of these in a single activity? If yes, please share with me the source code.
From another SO answer:
To reduce the complexity and simplify the language, Android does not support multiple inheritance as it's based on Java programming language. Hence you can't extend both ActionBarActivity and YoutubeBaseActivity in a Single Activity.
The solution is pretty simple: use the YouTubePlayerFragment class. This does not pose any requirement on the Activity, leaving you with plenty of options for your theming.
Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity.
Note : ActionBarActivity is deprecated, use AppCompatActivity.
Instead of having the Youtube player in the Activity (extending YoutubeBaseActivity), make your Activity extends from AppCompatActivity and use a YoutubePlayerFragment inside the AppCompatActivity. You will be able to use all the features of AppCompat with your Youtube video.
If you REALLY want to use BaseYoutubeActivity, you have to add an AppCompatDelegate in your own Activity extending the BaseYoutubeActivity and use it in every lifecycle method of your activity. Read the documentation of the delegate and read the original source code of AppCompatActivity to understand the delegate.
You cannot do that for now. you can use fragment instead of activity in your Activity that extends AppCompatActivity
Please refer to this answer. https://stackoverflow.com/a/30101931/4321808

By default android creating new project as ActionBarActivity

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.

Android ADT Bundle Creates appcompat and I don't what

I downloaded the ADT Bundle directly from android website for my mac. Everything was fine until I created a new project, and eclipse created my project as well as a folder named "appcompat_v7".
But I don't it, because that way I can't follow the tutorial, since you can't extend a method more than 1 time for example:
public class MainActivity extends ActionBarActivity
What can I do ? I can't follow any tutorial. I was trying this one (https://www.youtube.com/watch?v=E780gbh6vLU) and I can't because I have that appcompat_v7 project. The same happens with this one (http://code.tutsplus.com/tutorials/android-sdk-create-a-barcode-reader--mobile-17162)
Please I really need help, for my university project.
You can make it extend Activity and use the android.app.* versions of various classes (Activity, Fragment, etc) instead. IN your styles.xml, make your base style extend an Android-provided theme (like Theme.Holo) instead of the AppCompat one.

Categories

Resources