Android FRAGMENT 'requestFeature must be called' - java

I am having trouble trying to use 'requestFeature' in my FRAGMENT. My code is below, I am trying to set the actionbar as an overlay.
#Override
public void onCreate(Bundle savedInstanceState) {
getActivity().requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
I do not want to use a theme because I don't want the whole app to be effected, only this fragment in particular.

You have to call requestWindowFeature() before the setContentView() in the Activity's onCreate() method. This Activity is the one that your fragment is a part of.

Related

Is setContentView method is a method of AppCompatActivity

I am learning to develop an android app and while developing an android app. I came across a method setContentView(R.layout.activity_main); in MainActivity.java. The MainActivity extends AppCompactActivity.
So setContentView(R.layout.activity_main); is the one of the method found in AppCompactView?

Toolbar style changes in Android 4 when activity called from a static method

Found a strange issue in the app.
Toolbar style changes itself just for one acitvity in whole app and only on devices below 5.0 Android version.
All the screens have the same <include> field for the toolbar.
The activities, where style changes, are all inflated with a static method:
public static void startAsRecent(Context context, TransferTemplate template) {
Intent starter = new Intent(context, TransferAnotherAccountActivity.class);
starter.putExtra(TransferCommonActivity.EXTRA_KEY_TEMPLATE, template);
starter.putExtra(TransferCommonActivity.EXTRA_KEY_IS_RECENT, true);
context.startActivity(starter);
}
Strange, that it works normail in devices with Android versions abowe 5.0.
What could be the reason of this kind of behavior? And where to look to fix this?
Thanks in advance!
Ok, the problem was in inheritance.
onCreate() method of inherited activities, where was the problem, called setContentView() first, and then super.OnCreate(). Still don't understand, why this affected only pre-lollipop devices, but the temporary solution is to make something like this in the superclass.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
//other stuff
}
#LayoutRes
public abstract int getContentViewId();

How the setcontentview is called?

I was tweaking the sample hello world app that android studio provides and found out that I cannot call the setContentView(R.layout.activity_main); outside any method.For example:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
setContentView(R.layout.activity_main); //compilation error
}
I know that I should not be calling setContentView outside onCreate(),but just for a reference I tried it out.I can figure out that this has something to do with Java and not android,but I can't seem to figure out the where the problem exactly lies.Any help will be appreciated.
As per activity life cycle onCreate() is the method called when the activity is first created
OnCreate() is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri , String[], String, String[], String) to

Android Outsourcing Navigation Drawer

how can I create a class that instantiates my navigation drawer correctly?
I want to outsource it because it is a lot of code and its always the same.
I already tried to create such a class. The problem is,
there are these two methods:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
that are overriden in the activity itself. Moreover the title doesnt change if the drawer is opened/closed and at last the arrow that is used as the button to open is not animated!
Thx for help!
First android studio provides a template for creating Drawer Activity. If you are going to create your project for the first time you can use:
or if you have created your project and you want to create a Drawer Activity you can find it like this:
Now if you do not like above approaches you can create an abstract activity class name it for example DrawerActivity and do all initialization in it. Then in every project you can add it and extends it. That is one time work. But you must design it carefully so it must be general enough and also meet your common requirements. For example it can have a protected DrawerLayout field so when you extends it you initialize that field after setContentView of subclass activity and all the stuff like onPostCreate and onConfigurationChanged are done in the DrawerActivity(superclass).

android execute method from another activity

I am calling from activityA a method that is on activityB.
Method is called from A to B properly but when I execute inside it webviewB.loadUrl(myUrl), app crashes for a nullpointerexception. Same webviewB method, if executed from activityB, works properly. Probably is done because context, when calling from A to B must be set, but how? Which is the best way to do it? Thank you.
ActivityA (tabHost) and ActivityB (tabcontent) are initialized properly and webview from ActivityB shows URL1.
Then when evet from ActivityA is recived, I need to load URL2 on webview from ActivityB.
EDIT:
when event is received on ActivityA,
ActivityB test = new ActivityB();
test.recalcula();
ActivityB,
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.vistaaib);
webviewB = (WebView) findViewById(R.id.webviewB);
webviewB.loadUrl(URL1);
...
public void recalcula (){
webviewB.loadUrl(URL2);
}
Either intents as suggested by Ortisi. Or Why don't you try creating a class, create that method in this class, And call that method from both the activity.
The best way to communicate between activities is using the Intent mechanism.
So just launch an intent from activityA in order to communicate to activityB that its loadUrl method should run.
Or for other solutions you can read this question:
Best way to accomplish inter-activity communication in an Android TabHost application

Categories