My app contains two fragments: list fragment, and detail fragment. Everything works fine.But when I change the orientation. everything gets messed up. I tried something like this to change orientation, it works in case of landscape to portrait, but doesn't work in portrait to landscape. Can anyone help me. here is my tried code:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Fragment listfragment = getSupportFragmentManager().findFragmentById(
R.id.fragment_container);
Fragment detailfragment = getSupportFragmentManager().findFragmentById(
R.id.fragment_container2);
FrameLayout fragmentLayout2;
fragmentLayout2 = (FrameLayout) findViewById(R.id.fragment_container2);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (listfragment != null) {
replaceFragment(listfragment);
}
if (detailfragment != null) {
replaceNewFragment(detailfragment);
}
}
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
fragmentLayout2.setVisibility(View.GONE);
}
}
You need to override onConfigurationChanged on your mainActivity that extends ActionBarActivity for this to work. Something like this should work:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getActionBar().setIcon(R.drawable.my_icon_land);//getSupportActionBar() for support library
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
getActionBar().setIcon(R.drawable.my_icon_potrait);
}
}
Call invalidateOptionsMenu() whenever you want to change the icons in the ActionBar. This'll redraw the ActionBar items and hence make a call to onPrepareOptionsMenu(). Inside onPrepareOptionsMenu(), you can check the orientation and set the respective layout for the menu items or just change the icons.
And since you want to recreate the menu on orientation change, you can call the invalidateOptionsMenu() method inside onCreate() because it will be called when the orientation changes.
Related
I added this code in my activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activityView);
}
And also added this line in the Manifest file
android:configChanges="orientation|keyboardHidden|screenSize"
The activity is not restarting, but the layout is not correctly loaded : not all componnent are showing and buttons click listner is never called ...
I'm using the same name file for all the layouts. but different content depending on orientation.
I even tried with different names : activityView_port/activityView_land and changed the code to:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activityView_port);
} else {
setContentView(R.layout.activityView_land);
}
}
but nothing is working ...
any help would be appreciated :)
setContentView(R.layout.activityView_land);
please dont call above method on configuration change. instead you can try with use some flag for determine what layout has to be set and call reCreate() method.
If don't want to destroy the current instance, better you can try removing and adding of views with animation.
MainActivity inflates a fragment like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.dashboard_fragment_container, df, TAG_DASHBOARD_FRAGMENT)
.commit();
But when the screen orientation changes, I wish to remove(destroy) this fragment.
Any easy way to detect when the screen is about to change, so that I can remove inflated fragments?
Try using method onConfigurationChanged(). It will detect screen orientation change.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
//remove fragment
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Set these condition in onCreate(), because orientation change will call onCreate() method again:
if(Activity.getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT){
getSupportFragmentManager().beginTransaction()
.replace(R.id.dashboard_fragment_container, df, TAG_DASHBOARD_FRAGMENT)
.commit();
}
Let me know if this works.
Fragments usually get recreated on configuration change. If you don't want this to happen, use
setRetainInstance(true); in the Fragment's constructor
This will cause fragments to be retained during configuration change.
Docs
Now When the Activity is restarted because of a orientation change, the Android Framework recreates and adds the Fragment automatically.
if u want to remove fragment during configuration change use:
In Activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
Also in Manifest:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Now in onCreate() of Activity remove Fragment using:
Fragment f = getSupportFragmentManager().findFragmentById(R.id.content_frame); //your fragment
if(f == null){
//there is no Fragment
}else{
//It's already there remove it
getSupportFragmentManager().beginTransaction().remove(f).commit();
}
I have an Adapter class and a MainActivity. The Adapter class displays the list of children names and beside the names, it displays the time they arrived to the school. Problem is when I rotate to landscape, the time values are lost.
I considered adding in my Manifest under MainActivity
android:configChanges="orientation|keyboardHidden|screenSize"
What happens is that the time values are not lost in landscape mode but the look of the layout appears same as that of portrait mode. In real, the landscape layout looks a bit different from portrait layout.
What do I do in this case in order to obtain time values and also maintain the look of the landscape layout.
There are 2 options:
use onSaveInstanceState(Bundle outState) method in your Activity or Fragment - to save your data and restore them after rotation (in onCreate(Bundle savedInstanceState) or onRestoreInstanceState(Bundle savedInstanceState))
Example:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("time_data", (Seriazable) mTimeList);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (savedInstanceState != null) {
// restore value of members from saved state
mTimeList = savedInstanceState.getSerializable("time_data");
}
...
}
use android:configChanges as you already use to handle changes by yourself but inflate landscape layout after rotation
Example:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
I prefer the first option and also this proposed in Android guide.
you can simply set screen orientation to portrait to your perticular activity so it cant be rotate. (if you want)
like this way:
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
Well, I wanted to save value of a variable contained in a dialogFragment when the screen is rotated in Android. I've tried every method I could find on the internet, and none of them has worked for me. Some kill my application, and others simply were not doing anything.
I need a real and effective way to save the value of an EditText that is reset when the device screen rotates. The EditText is in a DialogFragment turn this into a FragmentActivity.
thank you very much
Set your fragment's retaingInstance flag to true:
http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)
This prevents Fragment instance from being recreated.
Also be sure you don't recreate the fragment all the time:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (savedInstanceState == null) {
Fragment f = new Myfragment();
f.setRetainInstance(true);
getSupportFragmentManager().beginTransaction()
.add(R.id.container, f))
.commit();
}
}
Since View state is preserved during Activity recreation and your are keeping the same Fragment instance you don't need to save TextView value all the time.
I don't know if I understood your question, but you can capture the rotation as follows:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//save value
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//save value
}
}
If you want that your activity don't restart, you should add this into the manifest file:
<activity
.....
android:configChanges="keyboardHidden|orientation|screenSize"
..... >
</activity>
I just found out that you can not apply the methods to save bundles automatically to the dialogues, but these would apply to activities or fragments of these dialogues dependent. If I can fix it by code, will put the solution here. thank you
In my app, I have a FragmentActivity with multiple Fragments all in portrait mode except for one specific Fragment. I move between Fragments through footer Views in the FragmentActivity.
I have a different layout (actually have the same name but different widget Views) for this specific Fragment when there is an orientation change. When in landscape mode, I want to remove specific widgets from the Fragment layout View and the footer View of the FragmentActivity and when back in portrait, add everything back.
I have saved all the data I need in onSavedInstanceState() in the Fragment, but where best should I possibly test for an orientation change so that I can restore the Views appropriately?
Not sure I can override onConfigurationChange() and test there in my Fragment because I don't have the android:configChanges="orientation" in my android Manifest.
Keep this check in onCreate(). When you rotate your device the app is restarted. So onCreate is called again when the app restarts.
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
// Landscape
}
else {
// Portrait
}
You can control the orientation change by overriding onConfigurationChanged function of your fragment as well.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE){
Log.v("TAG","Landscape !!!");
}
else {
Log.v("TAG","Portrait !!!");
}
}