Today i met with the serious problem. Actually i have 5 activities in my activity group.
from 1st activity i go to 2nd and from 2nd i go to 3rd and so on....
and on the 5th screen when i press back key i came to 4th and so on....
When i again go to the same process it displays me the previous displayed data as well on the screen. m not able to find the sollution for the same.
i need that every time i follow the process it will show me the new data not with the previous one data.
pls help me.
i cant paste the whole code. its 5 activities.
I don't think you mean "activity group," which is something else entirely. I assume you're talking about the task stack. What you probably want to do is override the onRestart method on your activity, setting it to call whatever method it is that you use to refresh your view's underlying data. You may want to do that in conjunction with a member variable "dirty" boolean flag you set on your activity before you start another one so that it only triggers if you're coming back from another activity (and not when the user switches to another app and back to yours).
You can override back button of Android .
In that method you can clear all views from stack and set first view on screen.
Below is Code from my app this you can use as per your requirement.
#Override
public void onBackPressed() {
super.onBackPressed();
Tab2GroupActivity.group.back();
}
In Tab2GroupActivity class
public void back() {
if (history.size() > 0) {
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
Here history is my list which contains all opened views in it.
this solved my issue.
Related
I am calling a Master/Detail activity in android by clicking on a button placed in another activity (UserActivity). The strange thing is, that if i click the back button in the Master/Detail activity, I loose the Data in the state of the UserActivity. It wants to execute the onCreate Method again.
If I click on the login-button in the LoginActivity where i am redirected to the UserActivitiy and i go back with the Back-Button of the "Smartphone", the username and password i typed are still there. So there i do not loose the data.
Is there a difference between the back-button of the Smartphone and the back-button at the top of the program? I am a bit confused now and i know how to persist the state of the Activity. But my question is, why i am having this behavior on the one side and on the other not.
Just in case you will leave the question like this and don't add code:
What will probably help is checking for the amount of items that are already there. When I ran into this issue, the onStart() got called so quickly that it seemed to me that the Activity has lost the data. Actually it DID have the data, but calling onCreate/onStart (I'm in a Fragment) NULLed it.
What I did to avoid this is to check if there is a need to load items in the list. If there is, it calls a method that contains what the old onCreate/onStart did. If there is no need to load data, it will just skip the step and live with the "old" data happily for the rest of its life.
#Override
public void onStart() {
super.onStart();
if(DummyContent.ITEMS.size()<3){
initializeApp();
}
}
void initializeApp(){
videoTitles=new ArrayList<String>();
videoUrls=new ArrayList<String>();
. . .
}
I'm currently working on a project that has an activity which is consisted of two fragments.
The first fragment shows a custom expandable list. Every row is created from a custom layout that has a checkbox in the right side of it.
The second fragment shows more details about the clicked row from the list. In order to open the second fragment, the user has to click on the row. The checkbox is used for another reason.
So, what I'm trying to do is to display these two fragments side by side only when the application runs in tablets. When the app runs in handsets and the user presses one row, the second fragment should be displayed on top.
Furthermore, I have an action bar at the top of the screen which has implemented the usual back button.
The problem exists when I open the second fragment when I have already selected some checkboxes. When I press the back button, which navigates me to the first fragment, the checkboxes will not be checked.
The onSaveInstanceSate method is obviously not called (as the parent activity is not getting paused), so I can't save the ArrayList that stores the checked rows.
Last but not least, the fragments are being added dynamically.
The question
How can I properly implement the back button so when the user uses a
tablet, the back button should be used in order to close the activity, or a
handset, so the back button should be used as a navigation back to the first fragment with the ability to restore it's previous state?
if (mFragmentManager.getBackStackEntryCount() == 0) {
LogUtil.d(TAG,
"home fragment" + mFragmentManager.getBackStackEntryCount());
this.finish();
} else {
mFragmentManager.popBackStackImmediate();
}
try this should work , happy coding
My first idea would be to create a boolean in the resources of your project: in the "values" directory, your boolean would be false, for example, and in your "values-sw600dp" and "values-sw720dp-land" directory, the boolean would be true.
Then, in your code, you would check the boolean (using R.boolean.your_boolean) to know if this is a tablet or a handset.
Then, with a simple if/else, you would implement your code, depending on the value of your boolean...
if(yourBoolean){
//We are on a tablet
finish();
}else{
//We are on a handset
//Your code to navigate back...
}
You need to (1) detect if the user is on a tablet and (2) control the back function accordingly. I'm not sure how you're currently detecting whether the device is a tablet but a very easy method is described here. It involves a boolean resource that you can access when customizing your back function to determine the device type.
What I would do is override onBackPressed in your hosting Activity and control back function from there
#Override
public void onBackPressed(){
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize){
moveTaskToBack(true);
} else {
//handle fragment back stack
}
}
The info on handling the back stack and replacing the fragment is here in the android docs. I will update that section later but I have to run for now.
I didn't know how to phrase the question properly so let me explain.
I have noticed that on the android platform that when you press two buttons in one activity in quick sequence (press one button then the other before the activity has a chance to leave the screen) that two activities are called one after another. It is not visible while it happens but if you press the back button then the activity that was called with the second button leaves (finishes) and the activity that was called with the first button shows up. You have to go back again to go back to the calling Activity. So you have to press back twice to get to Activity 2's parent activity.
So I want to know if this is a problem for others and if so how would you go about fixing that. Or do you think this is not much of a problem.
You can call finish() after startActivity(your_intent); that way the activity you are leaving finishes and you get to the next one, so you dont have to press back twice to get to it's parent Activity.
I have figured out how to pass this problem. I have a base activity class that every other activity in my application inherits from. I override
onResume with:
#Override
protected void onResume()
{
super.onResume();
setCanStartNewActivity(true);
}
and startActivityForResult with:
#Override
public void startActivityForResult(Intent intent, int requestCode)
{
//Does not allow starting a new activity unless previous activity returned
//This is a trick to stop multiple simultaneous button presses starting multiple
//activities simultaneously.
if(!canStartNewActivity)
return;
setCanStartNewActivity(false);
super.startActivityForResult(intent, requestCode);
}
This basically makes sure only one new activity can be started for a button press. If you let go of two buttons at the same time only the button released the earliest (even by 10 microseconds) will be fired and the other will still send the intent but it will not fire until the first fired activity returns or the app dies.
I have 2 activity when I press the image on 1 activity it launches the 2 activity. Each time when 2 activity launch it triggers the http request and form the table.
It works fine but when i press "back" button and then again start the application and came to the 2 activity.
It doesnt clear the previous data of table and add the data which came from http request. I didnt use a database its just a xml tablelayout.
Simply I just want to start my application from initial state.
I dont want the android system to remember the activity's previous state when I backpressed the whole application.
Please help me on this.
then may be i think you should completely close the application on back press so your problem will be solved.
this is the code :--
#Override
public void onBackPressed()
{
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
this method kills the application and Ofcourse your problem!
In manifest, for second activity write android:noHistory="true", then it won't remember history.
if i'm getting correct (from your question) , You need to check the views before making http call e.g let us say u have a textview there in table layout, then check like this before making the http call:
if(getthevalueoftextview!=null){
textview.setText("");
}
I would like to create a button in my game for the user. The button will quit the application.
Please can someone tell me if there is a way to do this.
Thanks.
Edit:
I have an activity which uses another activity with a class that extends Android.app.Application using set and get methods.
Simply using the back button switches the activities until it goes to the beginning.
I go in between these classes 20 times.
Thats why I needed a back button. But I guess there isn't so I will have to do it the long way and set everything back to the first state on quit. Thanks
There is not a way to make quit button. And there is good reason for that because the Android experience is having the back button do the closing. So you just to make the back button exit back to the home page. To do that you need make sure that your current activity is the only one oh the history stack. Then you can create a button that just calls finish(). Hope the detail explanation helps.
You probably want to mange the activity stack better.
If you look at Activity and Task Design Guidelines
it might help.
Setting the flags when you start each activity is probably the key, code such as
Intent i = new Intent(this, Whatever.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
will limit the number of instances of 'whatever' to one only. ( A different flag might be more appropriate for you depending on how you want your app to run, read up about them all)
Try this:
public void quit(View view) {
if(Build.VERSION.SDK_INT>=16 && Build.VERSION.SDK_INT<21){
finishAffinity();
} else if(Build.VERSION.SDK_INT>=21){
finishAndRemoveTask();
}
}
If I read your full question, you are looking for a Reset button not exactly a quit button. I had a similar issue... the next and previous takes only one step back at a time. I wanted to go back to the very beginning. The way I acheived this is to have a class to manage the pseudocursor.. basically an int that represented which resource to pick (I used a singleton). In the main activity's Menu (android.view.Menu), I added a reset/go to beginning option. This will simply reset the pseudocursor. In my activity class's onResume(), I had the code to get the resource from the singleton. So no extra coding was required there.
Instead of having this option under Menu, you can always have a button in UI which does the same thing.