i have a gridview which is a calendar. once the user selects a date and decides to go to another activity , upon return the day he had previously selected should remain selected.
upon selecting a day the image of the gridview cell that was selected gets changed. now when the user returns , if i can simulate the onclick event , then i would not have to write extra code.
in summary, i need to simulate a onclick event on a gridview cell via code. how do i do it?
thank you in advance.
You do not need to simulate the click. You just have to store somewhere(shared preferences is ok) the day, which is choosen. If there is no such value, then no day is choosen.
So, all you need to do it to save the day and read the saved value in your onResume() method.
If want still reuse onClick, just move the code from onClick() method to the separate one, and call it with your onResume().
Related
I wonder that if I have two buttons, and every time I press the first button I call the method secondButton.setOnClickListener(new OnClickListener). My question is when I set the new listener for the second button every time I press the first button, what happens to the old listener is it still in the memory ?
Activity owns button, button has a listener that is owned by Activity, if both can't be accessed from root, then theoretically they should be GCed.
there is a better way.
secondButton.performClick();
https://developer.android.com/reference/android/view/View.html#performClick()
Hope it helps.
I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo
I have a ViewPager with a FragmentStatePagerAdapter and the starting page should be somewhere in the middle. But when the user goes to another Activity and then comes back, the page number should be saved, so that he returns to the same page. There is probably an easy way to get this done, but I can't find the solution.
Usually the current page number will be saved, so this is not the point. The problem is that I don't know where to put the code that sets the starting page at first, because when I put it in onCreate, the starting page will be shown even if I come back from a different Activity.
I also tried saving the number in a variable in the onPause method:
this.currentPageNumber = viewPager.getCurrentItem();
But next time when onStart() is called, the variable currentPageNumber is null, so it seems that variables are not saved after onDestroy().
I could use a static variable, but it feels wrong. Is there a better solution?
EDIT: Sorry, I didn't make clear enough, that I want the saved page only be opened, if I come back to this Activity after I launched it. Every time I start the Activity from the launcher, the starting page should be shown and not the saved page.
EDIT 2: The behaviour I want to have is exactly the same as the Google Calendar app has when you open the day or week perspective. If I open this app, the current day will be shown. If I swipe to another day, then open settings, then press back, this other day is still be shown. But if I restart the app, again today will be shown.
After you have initialised your viewpager, use this method :
viewPager.setCurrentItem(int position)
Or this :
viewPager.setCurrentItem(int position, boolean withAnimation)
You can save the last position by using SharedPreference in the onPageSelect() method where you can get the position of each page. You have to implement the OnPageChangeListner in order to have this method.
Edit
Your question wasn't clear :
Sorry, maybe I didn't express my problem well enough. I want the starting page to appear everytime I start my app. But if I enter the activity by the back-button (for example if I opened my settings activity and then go back) the last viewed page should be shown. The solution provided in the link will lead to the safed page everytime I open the app
I don't know why you want to change this, it's a normal behavior, read this.
But if you insist, you can always use setCurrentItem() method in the onResume of your Activity/Fragment, thus the first page will always be shown after your Activity/Fragment gets resumed.
Edit 2
That can still be done by setCurrentItem. In your adapter, try to detect the index of the page of the current day. Create a method that returns that field from outside the adapter. And then after you have initialised your ViewPager,
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(adapter);
setCurrentItem(adapter.getCurrentDayPosition()) // or something like that ...
The method in the adapter :
public int getCurrentDayPosition() {
return this.currentDayPosition // this a field of the adapter.
}
I have the same requirement to show the default page whenever the onCreate of the ViewPager hosting Activity is called and show the current page for all the other cases including the motivating case: when user navigate back from the Activity started from the current page (onCreate won't be called in this case). My solution is based on the time spend between onCreate and onResume.
If the time between onCreate and onResume is shorter then a threshold (I use 1s assuming onCreate could be finished within 1s which we really wanted for the good programming practice), then we believe this is the first time the onStart/onResume is called right after the onCreate, otherwise we believe it is about all the other cases including our motivating case.
Then we only need to set the current timestamp in onCreate and do the time comparison in onResume to make the decision to set the current item of ViewPager to default or current. Note that we need to save the current page number in onPause in SharedPreference.
I am not claiming this idea is perfect but it works fine for me so far. Let me know if you are interested in the implementation and I will post my code upon your request.
i think this solve your problem , because first time you launch your activity there is no instance saved (savedInstanceState) ...
read comments in the post ...Android :Save View Pager State ...
In Activity1 have 1 button ,if user clicks on button it goes to Activity2.here i want to change button flow based on result in activity10,i.e if result is OK in Activity10 then Activity1 renders then user clicks on button it should show activity5(After reopened the app also app should show this result).I tried by using Bundle but when i reopened the app first scenario executing,it can be achieved by using shared-preference by taking a Boolean variable but dont know how,suggest me with piece of code to handle this multiple activities with shared preference?help me
From any place you can do
int prefVal = PreferenceManager.getDefaultSharedPreferences().getInt("myint", defVal);
and
PreferenceManager.getDefaultSharedPreferences().edit().put("myint", 100).commit();
Based on the last stored value you, probably, can figure out what to display.
I am working on an android app,coming to the point, one activity in the app has two edit text fields, the first field gets active that is gets enabled when the activity is started, and it is only after the first text field is filled, the second field becomes active. This part work correctly.
Now what the problem is when the user fills the second text field and presses the back button on the device, the second text field should get saved and should become inactive, also when the user again goes to the same activity, the second text field should remain inactive,and should get active that is editable only after the user double taps the second edit text field.
I know that database would be needed for saving the data entered in the second field,but even if that is used how should I resolve the problem above, I'd really appreciate your help.Thanks.
over ride this function onBackPressed(). In this function, write your code to "save" the text box contents and onResume(), you can re-load the text with the saved value.
override
onPause()
and
onResume()
to do your task.
You could aslo try startActivityForResult()