I have two activities.
In the first one I have a button like this :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this, Activity2.class));
}
});
Then in my SecondActivity, I have a EditText.
But when I tap on the EditText, the keyboard is well showing but I can see the view of the FirstActivity during the keyboard slide animation. Is that normal ? I don't think so .. So I'm trying to understand what I have done wrong ..
Manifest.xml
<activity
android:name=".activities.Activity1"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar"/>
<activity
android:name=".activities.Activity2"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="adjustResize"/>
Thanks in advance !
If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
onDestroy() is the final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
And for your question you have a android:theme="#android:style/Theme.Translucent.NoTitleBar" which display transparent activity that is the reason to display your previous activity behind because the background is transparent and you have not finished the previous activity, so you see it. If you finish it i doubt you might see the mobile screen as well if you don't have any back states
I don't know your requirement depending on that you can,
Change the transparent Theme
Remove previous activity from back state
Go with 1 & 2
Related
the flow is like below.
MainActivity -> Second Activity
Press home key then device main screen shown.
Enter to app box and click the application icon.
I expect the second activity should be launched but main activity launched.
is there any way that I control it -?!
The default Android behaviour when you press the home button is that the current activity is pushed onto the stack, unless you called finish() in your activity. Whenever you open your application it will launch the activity that's on top of your stack.
I'ts based on your system memory. If you don't have enough memory to keep your activity backgrounded then Android will kill your activity and its state to recoup some memory. I'm thinking that perhaps your phone (or emulated device) is running short of memory.
Check if you are using for that activity, any android:noHistory="true" within your App's manifest. Try removing it.
You filtered your main activity by
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
so it should get launched when you click on the icon in your launcher. I think you should handle your pages with fragments to have the behavior you want.
boolean isSentToBackground = false;
public void onResume(){
if(isSentToBackground) {
finish();
}
}
public void onPause(){
isSentToBackground = true;
}
I have some Activites that are started from my main Activity. When I go to one of these Activities, press home, kill the process, return to the app (where it resumes on Activity I left from), and then press back, it just closes my app instead of going back to my main Activity. I expect the back stack to be preserved. Does anyone know why this is happening?
I'm doing 3 things that might be related:
(1) My main Activity has launch mode singleTask
I tried removing this, but it didn't solve the problem. For example:
<activity
android:name=".activities.ActivityMain"
android:launchMode="singleTop"
android:screenOrientation="portrait">
</activity>
(2) I'm overriding onBackPressed() in the secondary Activity, but calling super.onBackPressed() when I do indeed want to go back. I also tried not doing this, but it also didn't help. For example:
#Override
public void onBackPressed()
{
Logger.d("ENTER");
ViewUtils.closeKeyboard(this);
//kv If it's a valid location, but hasn't changed, then just finish up
if (((FragmentLocation) getFragment()).isValidSpecifiedLocation()
&& !((FragmentLocation) getFragment()).sendUserSpecifiedLocation())
{
finish();
overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
}
}
(3) I'm setting the parent Activity in the manifest to my main Activity (I've also tried removing this, but it doesn't help):
<activity
android:name=".activities.ActivityLocation"
android:screenOrientation="portrait"
android:parentActivityName=".activities.ActivityMain">
</activity>
Any ideas?
UPDATE:
I tried recreating a very simple example with these conditions, but couldn't reproduce. I'm not sure what's causing this issue at this point.
So here is my activity.
<activity
android:name=".MainActivity_Hard"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/title_activity_main_activity__hard"
android:noHistory="true"
android:screenOrientation="portrait" >
</activity>
I marked it as android:noHistory="true" because I did not want the screen to be added to the activity stack.
But however when I lock the screen and unlock it. The activity disappears and the previous screen appears. This should not happen. Could anyone tell me why.
Note: It happens in ICS (Samsung Galaxy Note), but not with previous devices (Gingerbread,etc.)
Setting the noHistory attribute in your activity means that as soon as the user navigates away from that activity--bringing up the lock screen included--its finish() function is called automatically, thus destroying it and going back to the previous activity that spawned it.
This is typically used for splash-screen-type activities which, for example, acts only as a launcher and you don't want the user to be able to go back to the splash screen from the child.
If this is not your intention, then you should consider removing the noHistory attribute and maybe managing the activity lifecycle yourself. (For example, if you don't want your activity to be in the stack, then just call finish() just after it starts another activity.)
android:noHistory
Whether or not the activity should be removed from
the activity stack and finished (its finish() method called) when the
user navigates away from it and it's no longer visible on screen —
"true" if it should be finished, and "false" if not. The default value
is "false". A value of "true" means that the activity will not leave a
historical trace. It will not remain in the activity stack for the
task, so the user will not be able to return to it.
I don't know why. But if you don't want to add this activity to current stack. You can set android:launchMode="singleTask". When start this activity it will be added to it own stack.
You can see more here enter link description here
Hope it help :)
In AndroidManifest.xml put this permission tag above application tag.
<uses-permission android:name="android.permission.WAKE_LOCK" />
I have an app that contains 2 activities: A1 and A2. When I run the app it opens A1 activity. Then it opens A2 by pressing the button (I use startActivityForResult(intent, 0)). Then if I pressing Back it returns to A1. Everything is ok.
But if I am in A2 activity and switching to another application and then returning back to my app, then I'm pressing Back and the application exits instead of switching to A1 activity.
What can I do to prevent such situation?
The code that calls A2 from A1:
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), A2.class);
startActivityForResult(intent, 0);
}
Declaration in AndroidManifest.xml:
<activity
android:name="A1"
android:launchMode="singleInstance"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name="A2"
android:launchMode="singleInstance"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
In your activity over-ride the back button and do whatever handling you want..
#Override
public void onBackPressed() {
//Do whatever you want here on back button press
}
However in your case, if A2 is only getting started from A1 by startActivityFroResult() , then A1 must always remain in activity stack-trace..thus pressing back should always go back to A1.. unless a rare case when android os cleans up your A1 activity due to inactivity/high usage.
Are you calling either of the this.finish(); or the finish(); tag when you are calling the A2 via Intent?
You could also work on the onPause() and onResume() methods. Without any code provided in the OP, the answer will be based on guessing a bit. Do correct if I'm wrong in guessing. :-)
Also, update the OP with some code. And, you could also read up about an Activity's Life Cycle.
EDIT: The issue seems to be similar to this one here: Android is killing my application whenever startactivityforresult is called. Again, it boils down to saving the activity state, a part of Activity Life Cycle.
Try changing singleInstance to singleTask in your manifest. With singleInstance, Android will not launch any new instances into your task (i.e., activity back stack), which means that if the root Activity gets destroyed by the OS, it will not call A1's onCreate to recreate it.
The details are in the Tasks and Back Stack document under "Defining Launch Modes".
Note that once A1 gets destroyed, you will have lost all its state, so you probably want to implement onSaveInstanceState, which will allow you to save a Bundle that will be passed to you in onCreate.
Is there some reason applying a theme to an activity would affect the code function? I was under the impression that styles/themes merely affected appearance...
I have a listfragment (filled from a database) with a button at the bottom, upon the press of that button a new activity is launched to allow you to edit or add to the list/database.
Intent i = new Intent(getActivity(), Activity2.class);
startActivityForResult(i, ACTIVITY_EDIT);
This second activity works as it should and upon exiting back to the listfragment, said list is updated and the new item appears in the list.
Here's where I start having an issue...
Since the second activity is only a TextView, EditText and two buttons, I thought I would use the dialog theme to keep it from taking up the entire tablet screen unnecessarily. From the manifest file:
<activity android:name="Activity2" android:theme="#android:style/Theme.Dialog" />
This does achieve the result I was looking for as far as appearance of the activity goes, but upon exiting activity #2, the list in the listfragment is not regenerated/redisplayed. The additon to the DB is being done, as I can see when I re-start the app the item I added previously finally appears.
The above addition to the manifest is the ONLY change made.
Any thoughts on why this is happening and how to stop it?
Or you could add code in onResume() to refresh it. When it calls the dialog, it calls onPause() and then calls onResume() when the dialog is exited.
Your first activity isn't stopped and restarted when you launch a dialog, but it is when you launch a full screen activity. Try calling notifyDataSetChanged on the list adapter after the dialog activity finishes.