getWindow().setSoftInputMode not working as expected - android

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);

Related

How to work with AsyncTask when screen rotates

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!

android-how to save dada when activity re-created

I'm moving from activity 1 to activity 2 and send some data via intent .
It's ok and no problem ,but when I leave activity 2 and then come back , the intent is empty . For solving this problem I've tried this code :
#Override
protected void onSaveInstanceState (Bundle outState) {
outState.putInt("row_id", row_id);
}
#Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
int row_idss = savedInstanceState.getInt("row_id");
Log.v("this","restore" + Integer.toString(row_idss));
}
I tried to save it before I leave the activity and retrieve it on re-open .anyway , It didn't work and didn't log it either.
I tried onPause and onResume , strangely they didn't call either . I used this code to make sure it runs
#Override
protected void onResume() {
super.onResume();
Log.v("this","test");
}
nothing logged .
Could you help me to solve this problem ? I don't know what should I do to solve it
thanks so much
You are missing the calls:
super.onSaveInstanceState(outState);
super.onRestoreInstanceState(savedInstanceState);
Check the answer of this question, in this answer they're not using the method onRestoreInstanceState but just the onCreate to restore the data. You also could use onRestoreInstanceState if you need a different behaviour.

how to implement onRetainNonConfigurationInstance

i made a media player in android
it's working great but when i change the screen orientation, the activity is being restarted
i know that this question was already asked on stackoverflow several times
but none of the answers helped me.
i think i should use: onRetainNonConfigurationInstance
#Override
public Object onRetainNonConfigurationInstance() { ... }
but i didn't know the correct way to implement it
so if someone could give me a tutorial or an implicit example i would be grateful
I believe that onRetainNonConfigurationInstance() is deprecated. It will tell you to use Fragments instead. Here is a link to the Fragment documentation. Basically, you will put your UI and data into a custom Fragment, then use the FragmentManager to store an instance of your Fragment. Then, when the activity restarts, you can fetch your Fragment and reposition as needed.
Never mind that it's deprectated, it works fine. Simplest would be:
public Object onRetainNonConfigurationInstance() {
return this;
}
Then in YourActivity's onCreate()
public void onCreate(Bundle savedState)
{
YourActivity prevActivity = (YourActivity)getLastNonConfigurationInstance();
if(prevActivity!= null) {
// So the orientation did change
// Restore some field for example
this.myValue = prevActivity.myValue;
}
}
In my media player, in order not to re-create MediaPlayer completely, I did the following:
1) In AndroidManifest.xml added
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize">
2) Inside the MainActivity added
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setSize(mVideoWidth, mVideoHeight);
}
I hope this helps someone.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) no response at all

I have this code on my main activity.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.startactivity);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
...
But the screen still dim after a while, any clue why this is happening?
From the documentation for setFlags() (for which addFlags() is a convenience method):
Note that some flags must be set before the window decoration is
created (by the first call to setContentView(View,
android.view.ViewGroup.LayoutParams)
Meaning you should move your call to addFlags() to before you call setContentView(). #nandeesh posted this answer already but deleted it -- I am not sure why.
On the panasonic toughpad FZ-B2 enabling USB debugging did the trick. The screen goes on now.
Try adding the flag in onAttachedToWindow() method.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

Activity doesn't show in full screen

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!

Categories

Resources