I want to know what is the best way to handle a UI during a network call. For example -
I have a login screen with login button and email and password input fields. I enter email and password fields and press login button. I show a progress dialog. I subscribe now with RxJava and I have my observer in presenter. He tells the view to show a progress dialog. The API call is in progress. Stil my observable has not returned anything back. Now I press back button and my progress dialog is dismissed. I again press login button and a second call is done. So I can do this repeatedly and queue my requests.
What is the best practice to handle this scenario where user is allowed to make one request per once button click? I know there are several ways to do it in view such as greying out the button or using a progress bar which is non dismissable. But I am more interested in knowing how to deal this scenario in my model layer. I want my view to be as dummy as possible.
Why not override onBackPressed to cancel any in-progress threads?
Related
I am working on a small task for an android app. And I dont really know what are the things I need to do or things I should be careful for.
In a user's personal page, there is a button where the user can click in. Then it goes to a layout where there are two buttons "cancel" and "submit". Below them, there is a blank space where the user can edit his personal info. (Here the keyboard should automatically pop up).
After he clicks "submit", the info is saved and the app goes back to the personal page and the info he just entered will be displayed.
If a user already has some text for his info, after he clicks the edit button, the whitebox in the layout should has the info that he has previously entered. from that he can re-edit and submit again.
Any type of hints and guidelines will be greatly appreciated!! Thank you so much!!!
This sounds like a good place to use Activity results. You can make the screen where the user enters the information an Activity, and then start it by calling Activity.startActivityForResult(). Once the user is finished and clicks done, you store the result with Activity.setResult() and call finish() to close that activity. The original Activity that opened that screen will then get a callback in onActivityResult() with the data that you set in setResult(). This is a pretty common task so there should be plenty of examples around.
I have a dialog and when the user clicks the ok button in the dialog, the call goes from client to the server and then starts processing. In mean time when it is in the processing stage when the user tries to click anywhere on the dialog it is getting hanged and then once the process gets complete it behaves normally. So until the process gets complete i dont want the user to click the dialog, even though if he clicks the event should not be detected and dialog should not get hanged.
I dont want use progress monitor, is there anyway to handle this?
This is code I am using after OK button Pressed
`//Server call
startServerProcess(compsTable);
//Async to update UI
Display.getDefault().asyncExec( new Runnable()
{
public void run()
{
label.setText("");
}
});`
Even though the async call is used, when user clicks anywhere on the dialog it shows hanged and says not responding. Any help for this? –
Unless you are doing it asynchronously, it will behave like it does. the SWT is waiting until it gets the response back from the server, and during that time, whatever you do (e.g. click or do other actions) will not have any affect because it is not ready for user interaction yet.
You can run the job in a Thread, but ProgressMonitor was designed to give you a nice modal UI dialog telling you to wait. If you run a separate thread, you'll have to check if they click on the OK button twice, or some other element you left accessible.
I my opinion ergonaut's answer is correct and You should go with threads and asynchronous processing.
But if You insist to do it in one UI(!) thread then disable dialog parent composite, send, receive and process server's response. Then enable parent composite. This will block unnecessary events during processing.
parent.setEnable(false);
send(message)
process(recv());
parent.setEnable(true);
Be aware that user expects some kind of notification when something is processing. Without showing that app is busy user probably assume that application hangs and terminate it.
So using a GUI in java, I am trying to allow the user to press a transfer button which will initiate a method. This method will open up a separate GUI which will ask the user to enter information and then save it when the user has pressed a save button. What I want to do is pause the method after it has opened the separate GUI and continue once the user has hit the save button in that second GUI. Unless there is a better way, I believe that implementing a wait notify method is the best way to go about this but I have been unsuccessful thus far.
Firstly, DON'T (use wait/notify) for any reason within the Event Dispatching Thread. This will cause the UI to stop responding to input events and repaint request, effectively "hanging" your program.
In your case, it would be a simple case of using a modal dialog.
Check out How to make dialogs and Concurrency in Swing
i want to develop an android application to locate a mobile no.Obviously it cannot be done without user approval.
Basically i need the GPS coordinates of the other user.So my idea is to, after selecting a contact from the list,when i click on locate Button a automatic message/notifaction is sent to the other user asking for his/her permission to Allow/Deny.Once the user clicks on the allow button an automatic msg will be sent to the locator giving the longitudes,latitudes of the mobile which i can locate using GPS
If I understood you well, on the receiving end, a Service will be monitoring for Events (on Bluetooth or Wifi) and should popup a dialog requiring user to accept or deny a transaction.
You can quite easily make an Activity that polls for Event (or better, make a bounded background Service Message your Activity when a new Event has been detected) and display a popup AlertDialog. If you design it this way you will need your Activity to be active and running (i.e. displayed).
If you need your application to get user attention even when your application is not displayed, you will not be able to do so: you can not (nor should) directly start an Activity from within a Service. You will have to use Notifications and/or a Toast message to unintrusively notify your user that something requires his/her attention. Notifications appear in the status bar and can be used to launch your Accept/Deny Activity.
When the user opens the application, there is a screen with a button on it, which says "login."
The user clicks on the button, and a webview pops up to allow him to log in to the website.
After logging in (the app would need to know somehow), the webview would disappear, and then a list of usernames will pop up. (ListView?)
When the user clicks on one of the usernames, a webview of the username's profile will pop up. Of course, when the user pushes "back", it goes back to the list of usernames.
Can someone explain this to me in terms of Activity and Views? Am I using two activities to do this? Do I hide webview or listview when the user clicks between them?
I did the tutorial (notepad tutorial), but I'm still confused as to what is the best way to develop this.
Thanks
When the user opens the application, there is a screen with a button on it, which says "login." The user clicks on the button, and a webview pops up to allow him to log in to the website. After logging in (the app would need to know somehow),
Yo can do this with two separate Activity classes. I would put the WebView in its own Activity. This is easier than managing lots of different View objects yourself. Also, you'll get transitions between different things if you put each part in its own Activity.
You'll can launch the login Activity with the startActivityForResult() method, allowing it to return if the login was successful or not.
If you want to detect the login, you can monitor events in a WebView using a WebViewClient. You set the WebViewClient of your WebView using the setWebViewClient() method.
the webview would disappear,
Simply start the next Activity using an Intent and call the finish() method on your first Activity. If you do this then the use won't come back to login button Activity if they click back as it won't be on the stack any more.
What I'm not clear on is how long the login at the website will be valid for. You may need to set the flags on the Activities in your Manifest, to ensure the user has to log in again if they leave and then return to your application.
and then a list of usernames will pop up. (ListView?)
Use a ListActivity. This is an Activity which comes with the API designed for displaying a single ListView.
When the user clicks on one of the usernames, a webview of the username's profile will pop up. Of course, when the user pushes "back", it goes back to the list of usernames.
So use the onListItemClick() method in ListActivity to detect the touch and launch a new Activity containing the WebView to show the profile. As this is in a new Activity the back handling is all automatic.
Essentially it could all be done within one activity. I can try to easily demonstrate how it could be done.
When you run your app, Activity (AndroidManifest.xml) is called.
View (could be called, main.xml) is shown displaying a login button
Upon clicking the button, you launch your WebView
Successful login, switches the View to a custom view displaying usernames on a ListView (might be a predefined XML file you create named userlist.xml)
Using onClick on a specific username will launch a WebView regarding that specific username.
To return back to your application when the user hits the Back button, you might need to utilize onPause and onResume but I am not sure.
This may not be the best approach, or even a good one, but it might help clear up any confusion.
EDIT:
An Activity is what gets bound to the AndroidManifest.xml as the main entry point into an application. A View contains user interactive components within your Activity, such as a login button, displaying username's, viewing contents on the web (WebView), etc.
First of all I'd consider a different way of approaching this problem.
You could create your own login layout in Andoid and send your login data to the website. After doing this you should create a ListView of all user names. If the user selects one of these user names you should open a WebView.
The problem would be, that you would have to check whether the login was successful or not! Basically you need to do a HttpRequest, parse the output and check it.
If it's your website you want to login you could write a small wrapper for your login which returns true/false for your login. (php wrapper which returns xml or plaintext)
Anyway, if you really want to do the login via WebView you can check it via:
private void initWebView(final WebView mWebView) {
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Page = new WebView(YourClass.this);
Page.getSettings().setJavaScriptEnabled(true);
Page.loadUrl(url);
if(url.contains("OkDoLogIn.php")) {
/* Check if login was successful if true start new Activity */
}
return true;
}
});
}
With url.contains("OkDoLogIn.php") you basically check which url is about to open. In your case it would be the url or a part of the url of your login button.
Nevertheless you would have to check if your login was successful or not!
Edit: See 1st comment!