I defined a new Activity on my project and I have some trouble with fullScreen.
I defined in the manifest file like this:
<activity android:name=".Test"
android:launchMode="singleInstance" android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
.............
>
If I start the activity from another activity, I got the desired full screen. The problem is when I start this activity from a BroadcastReceiver - I need to open this activity inside a BroadcastReceiver something like this:
public void onReceive(Context context, Intent intent) {
Intent test = new Intent(context, Test.class);
test.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(test);
}
I tried like this too:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.test);
}
and no full screen if the activity starts from my BroadcastReciever.
Why I don't get full screen on this case? There is any way to request full screen after the Activity is created and visible?
I fond the issue. There is a method I omitted to add in question text - I didn't thought it's relevant. Because I want this activity to intercept (do not react) home button press, and for this reason I override onAttachedToWindow() method like this:
public void onAttachedToWindow() {
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
And here is the issue. Some times, because of this, my activity didn't get full screen. To fix this, I don't know if this is the best way, I added a delay to this code, like this:
public void onAttachedToWindow() {
handler.sendEmptyMessageDelayed(100,100);
super.onAttachedToWindow();
}
and the handler:
public boolean handleMessage(Message msg) {
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
and this solved my issue. I hope this help someone!
Related
I have an activity in my android app that calls an asynctask on onCreateView()
Here is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weeklyprofit);
fromDateTxt=(TextView)findViewById(R.id.fromDate);
toDateTxt = (TextView)findViewById(R.id.toDate);
GetXYPoints getXY = new GetXYPoints();
getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}
now my application needs to rotate, so i need to store data when rotate :
I have implemented this as below :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("from", fromDateTxt.getText().toString());
savedInstanceState.putString("to", toDateTxt.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
SystemClock.sleep(500);
fromDateTxt.setText(savedInstanceState.getString("from"));
toDateTxt.setText(savedInstanceState.getString("to"));
GetXYPoints getXY = new GetXYPoints();
getXY.execute(fromDateTxt.getText().toString(),toDateTxt.getText().toString());
}
So i recall the asynctask again with restored data, my problem is that the activity run again when rotate and the it calls onRestoreInstanceState so how can i prevent the activity from calling the first asyncktask?
In other way what is the best solution to store data returned by an asynck task when screen is rotate?
i would suggest you to to make sure you actually need your activity to be reset on a screen rotation (the default behavior). Every time I've had issues with rotation I've added this attribute to my tag in the AndroidManifest.xml, and been just fine.
android:configChanges="keyboardHidden|orientation"
source How to handle an AsyncTask during Screen Rotation?
In your AndroidManifest.xml add following for prevent reloading activity when orientation change
android:configChanges="orientation|screenSize"
So, you'll have something like this:
<activity android:name="Activity"
android:configChanges="orientation|screenSize">
</activity>
Hope it works!
In my Android project I want the softInputMode for just one fragment to be adjustPan.
Adding the following line to my manifest (inside the activity) works as expected:
android:windowSoftInputMode="adjustPan"
But following lines in my fragment do nothing:
#Override
public void onResume() {
super.onResume();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
#Override
public void onPause() {
super.onPause();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
}
Any ideas why that is and what could be done to fix?
You are setting the soft input mode for the activity, i'm not sure if it will work but try:
myFragment.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
EDIT:
I suppose you are working with the onPause and onResume of the fragment, have yout tried using the ones of the parent activity? The result might be the same thou, because some times they are connected.
Try to also set the android:windowSoftInputMode="adjustPan" in your Manifest. I know it looks redundant to set the soft input mode in the manifest and in your code but that's the only way it worked for me.
Implement in 'onCreate' method of your activity.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
You must do it like:
((AppCompatActivity)getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
I'm a totally noob on Android, is there a way to execute an app without a layout? The process would be like: Click app icon -> run some code (Without prompting any window) -> display toast.
The trick is to open a transparent activity, show the toast and finish the activity, which makes it look like only the toast is displayed because the activity which opened was transparent.
To do this you can do.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, messageToBeDisplayed, Toast.LENGTH_SHORT).show();
// finish the activity as soon as it opened.
this.finish();
}
}
Also you need to give a transparent theme to your activity by specifying it in AndroidManifest.xml, For which you can use NoDisplayeTheme provided by Android like this.
<activity android:name="TransparentActivity"
android:theme="#android:style/Theme.NoDisplay">
</activity>
Yes you can by adding:
android:theme="#android:style/Theme.NoDisplay"
in your activity in Android manifest.
Check this answer for more details.
Use this:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
this.finish();
}
}
and in manifest file add: android:theme="#android:style/Theme.NoDisplay"
I want to call my screen orientation from one activity to another to compress the code and also make it easier to call other functions.
public class Activity2 extends Activity{
public void Orientation(){
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
The main application will call the orientation from another class:
public class main extends Activity {
final Activity2 summon = new Activity2();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
summon.Orientation();
setContentView(R.layout.main);
}
}
Can this be done? Sorry for my bad English, this is my second language.
in onCreate() of your activities, do this for setting Landscape Orientation
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
and this for Portrait Orientation
`this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);'
and let Android manage your activities and use startActivity() method to start another Activity from one Activity. Every activity will have its orientation as per its configuration.
regards,
Aqif Hamid
First of all, you can not create Activity object like that.
To answer your question(if i understood it correctly), you can set the oriantation of an activity in the manifest like this
<activity
android:name=".MyLandscapeActivity"
android:screenOrientation="landscape"
>
</activity>
This looks very similar to my previous question because it's some sort of follow up. I was not very happy with the only solution given; also, the solution was for a problem slightly different from this one. So let me try to explain the problem again...
A notification is created at boot (with a BroadcastReceiver).
My app main activity is opened and the home button is pressed (the activity will be sent to the back stack).
I pull down the status bar and press on the notification previously created at boot.
That will start some activity, different from the main one.
I press the back button and the main activity is displayed.
This is not very different from my previous question... The thing is, "main activity" was just an example. I could have opened the app main activity and then opened the about activity through a menu option and pressed the home button. The back stack would now be MainActivity ยป AboutActivity. Which means that when the back button is pressed while in "some activity" (started by pressing the notification), we would be brought to the top of the back stack, that is, the about activity.
What basically want is to prevent any other activity to be opened when I press the back button while in "some activity" (again, started by pressing the notification). I want to be brought exactly where I was, that could be the desktop or some other app's activity, but not my app's MainActivity nor AboutAcitivity cause that's not where I was, those were in the back stack, "sleeping" in the background.
I have come up with a solution, but I don't think it's very elegant and I was looking for something more, well, elegant... If you have any other suggestion, please, let me know.
Anyway, this is my proposed solution:
// I use this class for public static (or public static final) members and
// methods
public final class AppHelper {
public static final String KEY_RESUME_FROM_NOTIFICATION = "resumeFromNotification";
private static boolean sResumeFromNotification = false;
public static boolean getResumeFromNotification() {
return sResumeFromNotification;
}
public static void setResumeFromNotification(boolean resumeFromNotification) {
sResumeFromNotification = resumeFromNotification;
}
}
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
#Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class AboutActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
#Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class SomeActivity extends Activity {
// This will be called when the notification is pressed and the activity is
// not opened yet
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
extractIntentExtras(intent);
}
// This will be called if the activity is already opened and the
// notification is pressed
#Override
protected void onNewIntent(Intent intent) {
extractIntentExtras(intent);
super.onNewIntent(intent);
}
private void extractIntentExtras(Intent intent) {
Bundle bundleExtras = intent.getExtras();
if(bundleExtras != null) {
// These intent extras are set on the Intent that starts this activity
// when the notification is pressed
AppHelper.setResumeFromNotification(bundleExtras.getBoolean(
AppHelper.KEY_RESUME_FROM_NOTIFICATION));
mRowId = bundleExtras.getLong(AgendaNotesAdapter.KEY_ROW_ID);
populateNoteUpdateFields();
}
}
}
I don't know, but this solution doesn't look very elegant to me (but it works as I expect it) and I'm looking for alternatives or for strong opinions on my proposed solution as an acceptable and good solution. Thoughts?
After doing some more reading perhaps this is the combination of flags you need:
Intent intent = new Intent(mContext, SomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(intent);
I think that should force your SomeActivity class to be launched in a completely new task.
When launching the Activity from the notification, you can control how the Activity you are about to open is put on the back stack, and what task it's associated with with Intent flags. You can try something like:
Intent intent = new Intent(mContext, SomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
If that doesn't work, try setting a few of the other flags until you get the desired behavior.
Do you ever want your MainActivity to stay in history? If not then my simple, crude solution is to finish the MainActivity when it is paused.
(Call this in your MainActivity)
#Override
public void onPause() {
finish();
}
This will ensure that your MainActivity is removed from history when you navigate away from it, and will never appear when the back button is pressed.
This could be used for AboutActivity as well.