Unwanted click events in Android after showing a dialog - android

CONTEXT:
I have a simple ListActivity (call it "Activity1") that contains a ListView and a number of menu items.
Clicking an item in the ListView shows a popup dialog.
Clicking a menu item button (let's call it "Button") in the ActionBar starts an Activity (let's call it "Activity2") using startActivityForResult()
PROBLEM: If the user touches a ListView item, generating an onListItemClick() event in the ListActivity and then really quickly touches Button, a onOptionsItemSelected() event is posted to the message queue and processed in due course. This results in the following odd behaviour:
User touches ListView item
User touches Button
User sees dialog generated by onListItemClick() for a split second
Activity2 started by onOptionsItemSelected() starts over top of the dialog
When user finishes with the Activity2, he returns back to the dialog, not right back to Activity1
This is very odd and unsettling. In my experience doing UIs in older GUI frameworks, the GUI framework never allowed the second event to post to the queue so you never had to worry about these things.
QUESTION: Is there a "preferred" design pattern to prevent the user from being able to press Button and have it start Activity2 when my app is already in the process of showing a dialog in response to the ListView item select?
Disable the entire Activity1 view hierarchy until the dialog ends??

Write in your ToS that users with 2 thumbs per hand should use the app with caution.
More seriously, maybe you can check if your dialog is open before starting your activity with either a boolean or Dialog.isShowing()

Related

Weird Shell page and ListView Navigation issue [Xamarin Forms]

I have a rather peculiar issue. My page has a list view with bunch of view cells containing appointment information. Now when user taps the list view entry they are redirected to appointment detail page using Shell.Current.GoToAsync(). On that page user can go back either through button press or through shell's back arrow.
Case 1.) When they press the Button "OK" - all works as intended, they go back to the page with listview, they can tap on any entry and it will open once.
Case 2.) When using back arrow, the page renders additional method run inside listview.itemtapped method, which I called OnListViewApptFinishedTapped. So every time the user goes back using the arrow, next time they press on any item from the list view, multiple appointment detail pages will open.
I hope I explained it well enough to get the gist of it. I have a temporary workaround with counter inside the ontap method, but it's definitely suboptimal.
Any pointers welcome!

Get blank activity after using navigateupfromsametask

I have two activities here. First activity only has a listview. After I click an item in the listview, the second activity is opened.If I click phone's hard back button, everything is fine, I'll go back to first activity and I can see my listview. However, if I click the back button on the action bar, navigateupfromsametask is called, I go back to the first activity, but I can't see the listview. I already set the first activity to be the parent activity of the second one in the manifest. Does anyone know why I can't see the listview? Thanks!

Alert Dialogs Over Activity Transitions

Assuming we have two Activities: List Activity and Detail Activity.
We have a "Save" button in Detail Activity which after saving, finishes the activity to go back to the List Activity.
I would like to know if we can have an alert dialog fired from "Save" function of Detail Activity so that it can stay over the transition of moving back to List Activity..
In other words, Can a Dialog exist outside the Activity? My understanding is that a Dialog is a child of an Activity and has to be destroyed if the activity is destroyed..
In iOS this is possible since the Dialogs are attached to the navigation controllers. Is this possible in Android? I am not considering fragments here..
If you simply want to display a message like "your changes have been saved" then in the Android world you would display a Toast. A Toast can even have a custom layout.
Another possibility would be to move your code to Fragments (which would be a good idea anyway) and then have an activity that acts as controller (i.e. creates and swaps the fragments). That should allow you to open a dialog while activating a different fragment.
You can choose two possible options:
You can stop the AlertDialog when the Detail Activity is dismmissed and then show it again in the method onCreate on the List Activity.
Another option is that you use a one single activity rather than two activities, and change the layout of the Detail Activity to the layout of the List Activity when you push the "Save" button.
There isn't an easier way to get what you want.

Android ListView - Unable to go back to ListView after calling onItemClickListener

This question is actually a 3 Part Question. Please suggest the valid solution for my problem.
Application Overview
I'm creating an application having a splash screen and a ListView. On click of the launcher icon, first the splash screen is shown and then after 3-4 seconds later, the splash screen calls the ListView activity and user is left with ListView items on the phone screen. On click of each item, respective textual content will be visible to the user. After reading the content user will click the "Back" button and will be returned to hte ListView items section again. If he/she wants to read any other item content, will be taken to that page on the click of that item. If not willing, then will click "Back" button on phone and application will close.
Part 1 - What I actually want to do
I want to show different textual content (not normal text but styled (bold/italized/containing images and all) on the click of different listView items. I can create the html pages and store them in my resource folder locally but don't know how to call those pages while calling onItemClickListener. I'm not sure if this is a valid approach to do this sort of work or not. Please advice. It's not working so far
Part 2 - What I'm doing instead now
Since I was not able to call the textual content so I wrote the content on some text editor, did all the fancy work on the editor and when the content was ready, I took the screenshot and then call that image instead while calling onItemClickListener. It's working fine and I'm able to see the image on click of my ListView item.
Part 3 - The problem I'm facing
When the image is shown to the user, on click of "Back" button, the application is closed instead of going back to the ListView Section. Apparently the image opening on click of the listview item is in the same activity as is the ListView itself. What I want is that user should be able to go back to the ListView section after he/she finishes reading the textual content.
Below is the code of the ListView Activity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.menulist);
String[] values = new String[] { "1. First Article Here", "2. Second Article Here", "3. Third Article Here"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.custom_listview, R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id){
//
if(0==position)
{
setContentView(R.layout.image0);
}
else if (1==position)
{
}
}
});
}
*The code works fine upto setContentView(R.layout.image0);
* But after this I want the application to return back to the ListView section on click of "Back" button but it closes instead. Please guide.
May be it solves by,
Using layout.image0 in another activity and call by intent when click on list item.
By use this, When you click back button you view your previous activity
When you press the back button, the current activity get closed and you get back to the previous activity on the stack. Given that you are not changing activity by pressing the list element, but only changing the layout, pressing the back button empties the stack and closes your app.
The cleanest way would be to launch the splash screen in another activity.
A couple of other ways to solve it are:
- add a stack of fragments
or (the dirtiest way), override the onBackPressed(), intercept it when your app is showing the splash screen and change back the layout to the listview.
I would prefer the other approaches.
If you use setContentView() method, the back button functionality will not work, since no new Activity was created, from which you can go back to the listView. I'd rather take the clicked item's useful information, put it in an Intent (link), and call startActivity() method, to start a new activity, to show content to the user. Then, if the user clicks the back button, the "details" activity will disappear, and the user again sees the listView.
You are using setContentView so its overriding the current view with ur new View in OnItem Click...Rather add a View Group to ur activity like a Relative layout...And when u click your Itme add a new layout to ur existing layout and on back remove the layout.
Or u can Use DialogView to dynamically add a view ..see below:
Dialog dialog = new Dialog(this, android.R.style.Theme_Dialog);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 0xff000000);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(Your VIew);
dialog.show();
Or at last you can start Another Intent using your current Activity..
Part 1 and 2
You can create complex and styled layouts using the XML files.
If you really want to use HTML, I advice you to use a WebView. You can store the plain html-string somewhere in your resources and use Webview.loadDataWithBaseUrl() to show it in your WebView.
You can make your choice depending on your functionality. If you have alot of different events to be handled (like clicking on a View), I would opt for the XML files. Because you can assign different listeners to seperate Views. When your functionality doesn't go further as it does now, only clicking a whole view. You can use a WebView as well.
Part 3
The problem is that you are abusing the setContentView-method. setContentView tells Activity which XML file it has to use. In your case, when the Activity is created, it will use R.layout.activity_main.
When you click your item, you tell the Activity.. Don't use the R.layout.activity_main anymore, but use R.layout.image0. The Activity will do this and your image will be shown.
This is not a good way to change layout though and will mess up your working flow. The reason that, when the image is shown, your application closes on Back is that there is only 1 Activity started. (Your stack contains only 1 Activity) You just switched the layout, but no new Activity is started.
Solution
Create 2 Activities:
Create a main Activity showing the your list and set the layout with setContentView(R.layout.activity_main). When your click-event is triggered, you will start the second Activity. Intent i = new Intent(...). You can also add some arguments to the Intent and access them in your second Activity to know which item was clicked and which image to show.
A detail Activity showing your content or in your case an Image. setContentView(R.layout.image0).
Solution 2
Create a popup to show your content. You can use a Dialog for that.

Android: ListActivity design - changing the content of the List Adapter

I would like to write a rather simple content application which displays a list of textual items (along with a small pic).
I have a standard menu in which each menu item represents a different category of textual items (news, sports, leisure etc.). Pressing a menu item will display a list of textual items of this category.
Now, having a separate ListActivity for each category seems like an overkill (or does it?..)
Naturally, it makes much more sense to use one ListActivity and replace the data of its adapter when each category is loaded.
My concern is when "back" is pressed. The adapter is loaded with items of the current category and now I need to display list of the previous category (and enable clicking on list items too...).
Since I have only one activity - I thought of backup and load mechanism in onPause() and onResume() functions as well as making some distinction whether these function are invoked as a result of a "new" event (menu item selected) or by a "back" press.
This seems very cumbersome for such a trivial usage... Am I missing something here?
Thanks, Rob
If the user hits the back button your Activity will likely get garbage collected.
If you properly start your activity from the menu with the different categories through an Intent, with passing the category etc. and then choosing the content in the onCreate method, you will get a new Instance of your Activity every time the user chooses a category, that will be destroyed after the user hits the back button.
This behaviour is perfectly fine. You don't have to cope with strange error cases and populating the list will take some time so the object creation time of the new ListActivity will be no problem. Try to program it as easy as possible for you and then test if there is a performance problem in the end of doing so.

Categories

Resources