I have two activity.
One of these is a sort of home activity (with some choises) and the other is the main activity.
Use do this:
<activity
android:name=".Home"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
And in Home.java i set:
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
protected void onStop() {
super.onStop();
finish();
}
In MainActivity.java I do nothing in method onPause() and onStop().
The problem is that if in MainActivity I set back, the app is in my background app and I can see the screen of MainActivity.
But when I restart the app, it restart with Home.
Well, how can I restart with MainActivity? Thanks
If you want your application to always launch from MainActivity you need to move the intent-filter to it in the manifest file.
<activity
android:name=".Home"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You are also getting this behavior because you are calling finish() in your onStop(). As described here you are forcing a onDestroy() in your onStop().
Try this:
#Override
protected void onPause() {
super.onPause();
system.exit(0);
}
App is crashing on orientation change,fragment has Fragmentstatepager
Crash log:
java.lang.NullPointerException
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.ja va:569)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281)
Solution: I fixed the bug by replacing getChildManager() with getSupportManager()
Write following code in your Manifest file...
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
and write following code in YourActivity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Hope this helps you..
you can lock the orientation by adding the following code
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should read and learn the official tutorial Android-Google about Layout.
For have a good showing, you have to add your elements (Button, TextView..) on multiple Layout.
After your App will be automatically resize depending orientation and size of your phone.
Or you can just locked orientation in your Manifest app
android:screenOrientation="landscape" or "portrait"
Good Luck.
EDIT :
Official :
http://developer.android.com/guide/topics/ui/declaring-layout.html
An other :
http://code.tutsplus.com/tutorials/android-user-interface-design-layout-basics--mobile-3671
I want my app to run in the portrait view so I made the following changes in the android manifest file.
android:screenOrientation="portrait"
It stays in the portrait view for a second and then it closes.. I had assumed this is the only change i had to make..but i also added this but it isnt working
android:configChanges="orientation"
Am i missing something?
<activity android:name=".Menu" android:label="COMEDY TRIVIA"
android:screenOrientation="portrait" android:configChanges="orientation"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="nik.trivia.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Try this adding android:configChanges to your AndroidManifest file in the activity tag
<activity android:name=".Fourth" android:label="Fourth" android:configChanges="orientation|keyboardHidden"></activity>
Hope this works for you.
When I turn the device from portrait to landscape at that time my activity is restarted. For that Activity I need to use both portrait and landscape mode.
How to Show my Activity?
add this line in manifest file android:configChanges="orientation|keyboardHidden" here
<activity android:name=".imgview"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and where u can override for restart . in my functions it's update listitems and some other function : example
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e("ScreenMode",""+screenMode);
if(screenMode==1)
{Log.e("ScreenMode",""+screenMode);
buildUrl(result,startPage);
showImageList();
}
if(screenMode==2)
{
showOneImage(forFunc);
}
if(screenMode==0)
{
startOMG();
}
}
To avoid getting restarted use android:configChanges="keyboardHidden|orientation" in your AndroidManifest for your Activity.
I have one of my activities which I would like to prevent from rotating because I'm starting an AsyncTask, and screen rotation makes it restart.
Is there a way to tell this activity "DO NOT ROTATE the screen even if the user is shaking his phone like mad"?
Add
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
to the <activity> element/s in
the manifest and you're done.
You can follow the logic below to prevent auto rotate screen while your AsyncTask is running:
Store your current screen orientation inside your activity using getRequestedOrientation().
Disable auto screen orientation using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR).
Run/execute your AsyncTask.
At the end of your AsyncTask restore your previous orientation status using setRequestedOrientation(oldOrientation).
Please note that there are several ways to access Activity (which runs on UI thread) properties inside an AsyncTask. You can implement your AsyncTask as an inner class or you can use message Handler that poke your Activiy class.
The easiest way I found to do this was to put
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
within onCreate, just after
setContentView(R.layout.activity_main);
so...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
In your Manifest file, for each Activity that you want to lock the screen rotation add: if you want to lock it in horizontal mode:
<activity
...
...
android:screenOrientation="landscape">
or if you want to lock it in vertical mode:
<activity
...
...
android:screenOrientation="portrait">
Rather than going into the AndroidManifest, you could just do this:
screenOrientation = getResources().getConfiguration().orientation;
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
... AsyncTask
screenOrientation = getResources().getConfiguration().orientation;
#Override
protected void onPostExecute(String things) {
context.setRequestedOrientation(PlayListFragment.screenOrientation);
or
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
The only drawback here is that it requires API level 18 or higher. So basically this is the tip of the spear.
Activity.java
#Override
public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// land
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// port
}
} catch (Exception ex) {
}
AndroidManifest.xml
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="QRCodeActivity" android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
User "portrait" in your AndroidManifest.xml file might seem like a good solution. But it forces certain devices (that work best in landscape) to go into portrait, not getting the proper orientation. On the latest Android version, you will get wearing an error. So my suggestion it's better to use "nosensor".
<activity
...
...
android:screenOrientation="nosensor">
Add the following to your AndroidManifest.xml
[ app > src > main > AndroidManifest.xml ]
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Example:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xxx.zzzzzz.yyyyy">
<uses-permission android:name="A-PERMISSION" />
<application>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation">
</activity>
</application>
</manifest>
The following attribute on the ACTIVITY in AndroidManifest.xml is all you need:
android:configChanges="orientation"
So, the full activity node would be:
<activity android:name="Activity1"
android:icon="#drawable/icon"
android:label="App Name"
android:excludeFromRecents="true"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Add:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
...
...
...
}
Prevent Screen Rotation just add this following line in your Manifests.
<activity
android:name=".YourActivity"
android:screenOrientation="portrait" />
This works for me.
android:screenOrientation="portrait"
on application tag <application
and
<activity
...
...
android:screenOrientation="locked">
If you are using Android Developer Tools (ADT) and Eclipse
you can go to your AndroidManifest.xml --> Application tab --> go down and select your activity. Finally, select your preferred orientation.
You can select one of the many options.
You have to add the following code in the manifest.xml file.
The activity for which it should not rotate, in that activity add this element
android:screenOrientation="portrait"
Then it will not rotate.
You can try This way
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itclanbd.spaceusers">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Login_Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Use AsyncTaskLoader to keep your data safe even if the activity changes, instead of using AsyncTask that is a better way to build apps than preventing screen rotation.