i'm having problems with my application, i added an manifest that must prevent screen rotation but when i rotate the screen it kills the app.
Here is my code:
public class avantdroidActivity extends DroidGap {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
super.clearCache();
super.loadUrl("file:///android_asset/www/redir.html");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
My AndroidManifest.Xml:
<activity android:name="org.apache.cordova.DroidGap" android:label="#string/app_name" android:configChanges="keyboard|orientation|keyboardHidden"> <intent-filter> </intent-filter> </activity>
What im doing wrong? Thanks!
Why are you calling?
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can just set your activity orientation in your AndroidManifest.
Just add android:screenOrientation="portrait" to your activity tag.
Related
Firstly, I have searched StackOverFlow for many hours before I post this question.
I tried to get onConfigurationChanged() called when user changes the theme in Setting but I've got no luck, event for other configs such as: Orientation, Screen size...
Please show me where I am doing wrong.
**Manifest file : use OnConfigChanges for handling config change event **
<activity
android:name=".Notification.OnConfigChanges"
android:configChanges="keyboardHidden|orientation|screenSize|uiMode"
android:label="#string/app_name">
</activity>
OnConfigChanges class:
public class OnConfigChanges extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_on_config_changes);
Log.d("onConfig","Yes");
Toast.makeText(this,"onConfig",Toast.LENGTH_SHORT).show();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d("onConfig","Yes");
Toast.makeText(this,"onConfig",Toast.LENGTH_SHORT).show();
}
I've MainActivity.kt where I show different fragments for different needs. At some point, I press button 'X' that calls startScanner() function:
private fun startScanner() {
IntentIntegrator(this)
.setOrientationLocked(false)
.setPrompt("SCANNING?")
.initiateScan()
}
Manifest.xml:
<activity
android:name=".MainActiity"
android:theme="#style/AppTheme"
tools:replace="android:screenOrientation"
android:stateNotNeeded="true"
android:screenOrientation="fullSensor"
android:windowSoftInputMode="stateHidden" />
Gradle.file:
compile 'com.journeyapps:zxing-android-embedded:3.6.0'
It does open scanner and everything, but in landscape mode.
Why is this not working?
There is a shortcut to do this. Just add this to the manifest:
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="portrait"
tools:replace="android:screenOrientation"
android:stateNotNeeded="true"/>
in addition of this answer https://stackoverflow.com/a/35465968/7666442
I found way to change Orientation of zxing scanner activity automatically when device Orientation change
Try this way
CaptureActivityPortrait
public class CaptureActivityPortrait extends CaptureActivity {
//Nothing in side.
}
CaptureActivityPortrait in manifest file
<activity
android:name=".CaptureActivityPortrait"
android:stateNotNeeded="false"
android:theme="#style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"/>
use this way in your activity
public class MyActivity extends AppCompatActivity {
IntentIntegrator qrScan;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
qrScan = new IntentIntegrator(this).setCaptureActivity(CaptureActivityPortrait.class);
qrScan.setOrientationLocked(false);
qrScan.initiateScan();
}
}
You can set the orientation programmatically (in your activity):
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I need to close an activity when a button is clicked. Unfortunately, when button is clicked, the activity does disappear but is still in the background. User can still select it and it comes back to front. What I need is the activity completely gone/destroyed.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
I searched on SO on related questions, however, none of them help with closing the activity completely. I already tried adding return, adding another broadcast listener and passing command to call finish outside onCreate. So at this point, the question is - is this really possible or is this how Android works, you can call finish() but it is still in the background and user can re-launch it.
Here is xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app1.test.myapplication">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT: Adding android:excludeFromRecents="true" does not solve the issue. Here are steps to recreate this, if anyone thinks it is a duplicate or already solved, please try and post comment/answer, I will accept it.
Open Android Studio
Create empty activity project.
Add a button.
Add code in MainActivity's onCreate for button click listener.
Inside click listener, call finish.
Run the app and click button and see if the activity is still in background or not.
Just give it a try.
In you manifest.
<activity
android:excludeFromRecents="true"
android:name=".Activities.SplashActivity"
android:theme="#style/AppThemeSubActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Now in your java code.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_search:
// Intent intent = new Intent(MainActivity.this, SearchActivity.class);
// startActivity(intent);
finish();
System.exit(0);}
}
put the extra line System.exit(0); after calling finish it works for me.
You need to put this in your XML manifest:android:excludeFromRecents="true"
in your Activity TAG.
<activity
...
android:excludeFromRecents="true">
</activity>
You could try this
android:excludeFromRecents="true", Use it in your manifest.
plz try this to go back
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
}
and this code to kill app process
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
I have an application with three activities.
MainActivity which looks like that:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
b.setText("click me to go to child activity");
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ChildActivity.class));
}
});
setContentView(b);
}
}
ChildActivity which looks like that:
public class ChildActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this) {{
setText("I'm the child activity");
}});
}
}
And OtherActivity which looks like that:
public class OtherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this) {{
setText("I'm other activity");
}});
}
}
In the manifest I have such declaration:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="pl.psobolewski.test.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="pl.psobolewski.test.ChildActivity" />
<activity android:name="pl.psobolewski.test.OtherActivity" />
</application>
Now when I start the application, it starts with MainActivity, from there I can go to ChildActivity, but there is no way to go to OtherActivity.
Then in the manifest I change this line:
<activity android:name="pl.psobolewski.test.ChildActivity" />
to:
<activity android:name="pl.psobolewski.test.ChildActivity" android:parentActivityName="pl.psobolewski.test.OtherActivity" />
Now I start again this application on my phone, which has Android API 16. It starts with MainActivity, there I can press the button and move to ChildActivity. Now the ChildActivity looks a little bit different than before: the logo on ActionBar has a little arrow-like icon (documentation calls it "a left-facing caret") which means it can be used to move up. But when I press it I don't go to OtherActivity - even though it is declared as the parent of ChildActivity - but to the MainActivity.
I find it contrary with the Android documentation which says:
http://developer.android.com/guide/topics/manifest/activity-element.html
"android:parentActivityName
The system reads this attribute to determine which activity should be started when the use presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder."
I also thought that adding android:parentActivityName attribute without calling setDisplayHomeAsUpEnabled would not turn the application logo into the up button - the documentation at http://developer.android.com/training/implementing-navigation/ancestral.html suggests so.
My question is: why the "up" button moves me to the MainActivity and not to the OtherActivity?
The Action Bar up navigation handler has been implemented in such a way that if the parent of current activity has no parent, then an Intent is created with ACTION_MAIN & CATEGORY_LAUNCHER to start the activity. This results in MainActivity being launched.
Have a look at definition of getParentActivityIntent() in Activity.java
To overcome this, in your ChildActivity.java override below 2 methods of Activity.
#Override
public boolean shouldUpRecreateTask(Intent intent) {
return true; // This creates a new task stack
}
#Override
public Intent getParentActivityIntent() {
Intent intent = new Intent(this, OtherActivity.class);
return intent;
}
If you don't want to override getParentActivityIntent, then you need to define a parent activity for OtherActivity in AndroidManifest.xml file, to overcome the earlier mentioned reason.
If you don't override shouldUpRecreateTask, since OtherActivity does not appear in history stack, it will remove all activities until the root activity of the task is reached, resulting in 'in-app home' behavior.
My application gets killed each time that it comes back from the screen-off-state. I fetch all the information that my app does, but I can't find out why it calls onDestroy. It's the first time I'm seeing this behavior in my applications.
My main activity extends tabActivity because it contains a tabhost. I've read that it has to extend it or it will FC. I'm not sure if my issue is related to this?! Oh and it implements Observer but this should be no problem.
Here are the logs:
07-21 09:57:53.247: VERBOSE/###(13180): onResume
07-21 09:57:53.267: VERBOSE/###(13180): onPause
07-21 09:57:59.967: VERBOSE/###(13180): onResume
07-21 09:58:00.597: VERBOSE/###(13180): onPause
07-21 09:58:00.597: VERBOSE/###(13180): onDestroy
07-21 09:58:00.637: VERBOSE/###(13180): onCreate
The crazy thing is that it calls the onDestroy the most times after the screen goes on again, and sometimes it has enough time to do this before the screen goes off. But after it goes on again it does the same again...
I hope that someone has a tip for me or any information on how to resolve this issue.
I'm not sure if this is important, but I use the android 2.1-update1 sdk for my application.
EDIT:
The application gets tested on a real Android Device.
Here is some basic code with all unnecessary lines and information removed:
package;
imports;
public class WebLabActivity extends TabActivity implements Observer{
#declerations
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("###", "onCreate");
setContentView(R.layout.main);
# initialize some basic things
}
#Override
public void onResume() {
super.onResume();
Log.v("###", "onResume");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.v("###", "onDestroy");
}
#Override
public void onRestart() {
Log.v("###", "onRestart");
super.onRestart();
}
#Override
public void onPause() {
Log.v("###", "onPause");
super.onPause();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.v("###", "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}
#Override
public void update(Observable observable, Object data) {
Log.v("###", "notifyManager.getWho() + " made an Update");
}
private void initializeSidebarTabhost() {
TabSpec 1 = tabHost.newTabSpec("1");
TabSpec 2 = tabHost.newTabSpec("2");
TabSpec 3 = tabHost.newTabSpec("3");
TabSpec 4 = tabHost.newTabSpec("4");
1.setIndicator("###");
2.setIndicator("###");
3.setIndicator("###");
4.setIndicator("###");
addIntents
tabHost.addTab(1); //0
tabHost.addTab(2); //1
tabHost.addTab(3); //2
tabHost.addTab(4); //3
tabHost.getTabWidget().setCurrentTab(2);
}
}
EDIT2:
Ok, I've tested my application without initializing anything, then with only extending activity, or without implementing observer, but my changes had no effect. Every time I set my phone to sleep, then wake it up, onDestroy() get's called?!
EDIT3:
Ok, I found out something interesting.
First here's my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tundem.###"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".###" android:label="#string/app_name" android:screenOrientation="landscape" android:theme="#android:style/Theme.Light.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As soon as I remove the screenOrientation="landscape", the application won't be destroyed each time that I wake up my device. I tried it more than 10 times but no more calls to onDestroy()
So I think that I will have to set this in code?! Any tips or pieces of code?
If you want to stop the destroy/create issue that is the default in android because of an orientation change and lock in one orientation then you need to add code and xml
In your activites code (notes about the xml)
// When an android device changes orientation usually the activity is destroyed and recreated with a new
// orientation layout. This method, along with a setting in the the manifest for this activity
// tells the OS to let us handle it instead.
//
// This increases performance and gives us greater control over activity creation and destruction for simple
// activities.
//
// Must place this into the AndroidManifest.xml file for this activity in order for this to work properly
// android:configChanges="keyboardHidden|orientation"
// optionally
// android:screenOrientation="landscape"
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
I had same issue. From ActivityA I called ActivityB. When I closed ActivityB, I expected ActivityA to be shown but it wasn't - it was destroyed. This issues was caused by following attributes of ActivityA in AndroidManifest.xml:
android:excludeFromRecents="true"
android:noHistory="true"
Because of them ActivityA was destroyed after ActivityB was started.
mikepenz,in your case if you realy need a android:setorientation = "landscape" means ,you dont need to remove it, just add these set of attribute android:configchanges = "orientation|Screensize" this wont destroy your activity... hope this helps you.