I have a ListView, populated from a XML. Everything is working fine, but when i change my device's orientation(Portrait<-->Landscape) it reloads the ListView. I want to stop it. I have used onSaveInstanceState and onConfigurationChanged. But it still reloads.
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
alrowDataView = savedInstanceState.getParcelableArrayList("name");
// Toast.makeText(MainActivity.this,alrowDataView.size() , Toast.LENGTH_LONG).show();
// lvrowDataView=(ListView) findViewById(R.id.list_xml);
}
else{
Toast.makeText(MainActivity.this,"NOdata" , Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
lvrowDataView = (ListView) findViewById(R.id.list_xml);
ParsingXml parsing = new ParsingXml();
After that general ListView operations...
and here is the code for OnSavedInstanceState and onConfigurationChanged.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(MainActivity.this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(MainActivity.this, "portrait", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelableArrayList("name", alrowDataView);
//Toast.makeText(MainActivity.this, alrowDataView.size(),Toast.LENGTH_LONG).show();
}
and these are the declarations
private ListView lvrowDataView;
private ArrayList<RowData> alrowDataView;
RowDataAdapter adapterRowData;
I don't want to use android:configChanges="orientation"
Also,in onSaveInstanceState the ArrayListis null!!!
Try this way
android:configChanges="orientation"
Is not working with latest android api 3.0+
You need to use like this
<activity
android:name="YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
/>
Related
I would like to not reload the RecyclerView after screen rotation from portrait to landscape.
I'm using onSaveInstanceState to save list from adapter, and restore it inside onActivityCreated
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(mAdapter != null) {
outState.putParcelableArrayList("key", mAdapter.getOriginalList());
}
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null) {
mList = savedInstanceState.getParcelableArrayList("key");
mAdapter.notifyDataSetChanged();
}
}
But there is problem, the RecyclerView is reconstructed after each screen rotation. What is wrong ?
You need to set adapter on configuration changed programetically then only it can be possible to set data without reloading.
Something like below,
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
if(mAdapter != null)
your_recycle_view.setAdapter(mAdapter);
}
How to save state of MapView in 3 situation 1) Rotate the device 2) Start new fragment and return to previous 3) Start new Fragment and rotate device. The following code resolves 2 Event:
public void onPause() {
Log.d(TAG, "onPause");
mMapView.onPause();
View view = mActivity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
super.onPause();
cameraPosition = mGoogleMap.getCameraPosition();
}
#Override
public void onResume() {
Log.d(TAG, "onResume");
super.onResume();
mMapView.onResume();
mMapView.getMapAsync(this);
}
This for 1 event:
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
if(mSearchString !=null && !mSearchString.equals("")) {
outState.putString("search", mSearchString);
}
final Bundle mapViewSaveState = new Bundle();
mMapView.onSaveInstanceState(mapViewSaveState);
outState.putBoolean("firstStart", false);
outState.putBundle("mapState", mapViewSaveState);
super.onSaveInstanceState(outState);
}
But in 3 event mMapView == null and NPE. How to solve?
You have to do the two main task while change the orientation of device
1>In activity at manifest file you have to put this code
android:configChanges="keyboardHidden|orientation|screenSize"
2> you have to override
#Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence("key", "save the value" );
}
and now check in on create of activity
if(bundle !=null)
{
String saveValue=bundle.getCharSequence("key");
}
now use saveValue after orientation.
If you want to prevent activity from restarting, simply add this to your manifest.
into your activity tag
<activity
android:configChanges="orientation|keyboardHidden|screenSize"
/>
EDIT:
This code will load the layout as per your orientation changes.
Add this code in your activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscapeView);
} else {
setContentView(R.layout.portraitView);
}
}
I need to clear error for EditText, when orientation screen change. I try in onStart() method set setError(null), but it doesn't work.
I tried it on the new project, but doesn't still work. Whem i click on button, field set error, when i change orientation i expect, that erro clear, but it doen't work.
public class MainActivity extends ActionBarActivity {
EditText text;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
text.setError("Error");
}
});
}
#Override
public void onStart(){
super.onStart();
text.setError(null);
}
}
UPDATE:
It seems you have to clear errors onPause instead of onResume/onStart:
#Override
protected void onPause(){
super.onPause();
text.setError(null);
}
The above works for me.
OLD:
Android will automatically handle EditText for you, and its states, on orientation change.
If you want to handle the state yourself you need to implement and override onSaveInstanceState and not call super(). You don't need to override onRestoreInstanceState unless you need to handle restoring for other stuff.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
}
This is what I will do when orientation changes.
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
text.setError(null);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
text.setText("Something");
}
}
And you can just the value of TextView as text.setText("") if you want to clear the "Error message" and if again the orientation changes, just set the text to anything you want.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
String phonenumber = phoneNumberLogin.getText().toString();
savedInstanceState.putString("PhoneNumber", phonenumber);
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
String phonenumber= savedInstanceState.getString("PhoneNumber");
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
super.onRestoreInstanceState(savedInstanceState);
}
I have one editText (for typing phone number). When in portrait mode I type the numbers and after that rotating the phones, the typed data is gone. I save data savedInstanceState but I have no idea how can I restore my typed data when I rotate the phone. Any help please.
On simple way is to avoid having your activity recreated on rotation. Change your AndroidManifest.xml to include android:configChanges="keyboardHidden|orientation" like this:
<activity android:name="MyActivity" android:configChanges="keyboardHidden|orientation"></activity>
Your layout will need to support whatever orientation the device supports. Also there are some other things to consider: Why not use always android:configChanges="keyboardHidden|orientation"?
try to move getting phone number from onRestoreInstanceState to onCreate method
or try something like this:
#Override
public Object onRetainNonConfigurationInstance()
{
return phoneNumberLogin.getText().toString();
}
and in onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
String phonenumber= (String)getLastNonConfigurationInstance();
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
}
is it possible to detect screen rotation? I mean - rotation only, which is clearly distinguishable from activity initialization from another activity?
The onXxx methods seem not to be useful for this, I have tried adding/removing a flag from the starting Intent (the removing seems not to be reflected, on rotate the flag is there), and have tried adding android:configChanges="orientation" for the activity in the manifest, however the onConfigurationChanged method seems to be called every second rotation... wired.
I guess I am missing something... but haven't found clear solution in the other related threads.
Any ideas?
Manifest:
<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>
Activity:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
....
}
try this link also
How do I detect screen rotation
Use onConfigurationChanged method to detect the screen rotation isn't good idea. Configuration Change in this activity wouldn't works correctly when user rotate the screen.
So I use this solution to solve this problem.
public class SampleActivity extends AppCompatActivity {
public static final String KEY_LAST_ORIENTATION = "last_orientation";
private int lastOrientation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
lastOrientation = getResources().getConfiguration().orientation;
}
}
#Override
protected void onStart() {
super.onStart();
checkOrientationChanged();
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
}
private void checkOrientationChanged() {
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation != lastOrientation) {
onScreenOrientationChanged(currentOrientation);
lastOrientation = currentOrientation;
}
}
public void onScreenOrientationChanged(int currentOrientation) {
// Do something here when screen orientation changed
}
}
But code still not good enough to use it in my project, so I applied this code to my own library (https://github.com/akexorcist/ScreenOrientationHelper).
compile 'com.akexorcist:screenorientationhelper:<latest_version>'
You can create base activity class like this
public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper.onCreate(savedInstanceState);
helper.setScreenOrientationChangeListener(this);
}
#Override
protected void onStart() {
super.onStart();
helper.onStart();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
helper.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
helper.onRestoreInstanceState(savedInstanceState);
}
#Override
public void onScreenOrientationChanged(int orientation) {
}
}
Then extend the activity with this base class
public class MainActivity extends BaseActivity {
...
#Override
public void onScreenOrientationChanged(int orientation) {
// Do something when screen orientation changed
}
}
Done!
Why don't you try this?
in onCreated get the orientation of the phone:
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
myOrientation = display.getOrientation();
then, override the method onConfigurationChanged and check if the orientation has changed:
#Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation != myOrientation)
Log.v(tag, "rotated");
super.onConfigurationChanged(newConfig);
}
Don't forget to add into the manifest android:configChanges="orientation" in the activity.