In Android, how would you develop this? (activity and views question) - java

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!

Related

Android task: click a button, go to a layout to edit personal info. click the submit button, save and display the info

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.

Android: open hidden webview activity or fragment

I'm developing an android app which shows a listview.
When the user clicks on an Item on the list, i need the app opens a regular activity with some details and, in background or in "hidden mode", opens a webview which load a URL (of my blog).
I need it because i want to trace which items are opened, analyzing the Google Analytics information on my blog; in this way, everytime someone clicks on an Item, i'll find the information on my blog.
Is there any way to do it?
The correct way to open a hidden webview is to call webView.setVisibility(View.GONE) on the OnCreate method

Android application design help, How to control which activity shows

Hello I require some advice on how to approach this problem,
bear with me as I am still new to android.
I have an activity that opens on application start and requires some information from the user, but next time the user opens the app, i want the application to open a different activity that will display the various information.
Kind of like facebooks app, where when you first run it, you have to login, and only then next time you run the app you are guided straight to the feed.
Any ideas how one could do this efficiently?
UPDATE: Ive stored the information via shared preferences and am now using a controller activity that decides which step to take.
So, Controller activity runs on start up, and decides whether to show a log in screen or whether to go straight to the information. But now im encountering a problem where i end up opening a blank activity (the controller) and then another ontop of that ( the decided activtiy). I dont want the blank activity to show, so its kinda of like a background process, any ideas?
Ideally you would have a main activity like a controller. Use a SharedPreference object to keep track of whether the user is logged in or not. So back in your main activity, read this value and if it is set go to your news feed activity else show a login screen activity. (as you do the check and redirection, you can show a progress dialog)
links for SharedPreferences
MobTuts
Android Developer

PreferenceActivity, how to make a Preference item that will navigate back through menus?

My overriding question is this: In Android PreferenceActivity, how can I write an OnClickListener which will have the same functionality as pressing the Android back button as I navigate through PreferenceScreen defined menus? That is to say, I would like users of my App to explicity see a menu choice "Back" which will bring them to the previous menu, or bring them out of the menu activity to their previous activity if they are at the root of this particular PreferenceActivity session.
The android developer documents tell us
Note that this XML resource contains a preference screen holding another fragment, the Prefs1FragmentInner implemented here. This allows the user to traverse down a hierarchy of preferences; pressing back will pop each fragment off the stack to return to the previous preferences.
And they are correct about that. I navigate happily through my menus by clicking on PreferenceScreen items to get to that screen, and using the Android back button to go Back up a level. But I'm not sure a casual user really understands the "Back" button, I know I didn't until I read about it in Developer docs. SO I would would like them to have an explicit Preference defined menu choice whos OnClickListener duplicates the function of the Android back button.
So I tried to put in a Preference in my menu that would go back. Having determined that a not Overriden onBackPressed in a my subclass of PreferenceActivity just referred back to Activity.onBackPressed() which merely calls finish(), I tried this OnClickListener:
private OnPreferenceClickListener clickFinishesSuccessfully = new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
finish();
return true;
}
};
As it turns out, this did NOT do the same thing as pressing the back button! Pressing this button always took me out of the PreferenceActivity entirely, back to the Activity from which I had called my PreferenceActivity. Specifically, it did NOT navigate back through my menus no matter how deep I was when I clicked it.
I am guessing here: When I have gotten to a submenu by clicking an onscreen preference which is really a PreferenceScreen, I am no longer in my own PreferenceActivity. I must be in some other Activity?
So my functional question: what can I put in my OnClickListener of my "Back" Preference to get the same function as the Android Back button navigating through my menus?
I think the casual user should know about the back button. The button is used everywhere so it might be a problem getting used in the first day but after that it's natural. Without being used to the "back" button I can hardly imagine doing the everyday tasks.
The preference you want to add just duplicates functionality and doesn't provide a consistent way with the rest of the system. If Google was considering back being an uncommon thing for casual users would have added that option in phone's Settings which is also a PreferenceActivity.

How to kick user to Login Screen Activity passing all other app Activities in the stack?

Lets say you have a web app related Android app that requires the user to login in order to use it. The user logs in, in the Login screen activity and then proceeds to other activities. Each time the user starts an Activity, the app checks his login credentials again (The credentials are stored in a central database somewhere).
As the user jumps from one activity to another, lets pretend that the user's login credentials are revoked from the service. The next time the user visits an Activity, the app will see that he no longer has access. The app should now kick the user back to the login screen Activity.
But since there is already a stack of Activities that the app has created as the user used the app, how do you get the login screen. Do you kill/destroy all Activities until you reach the login screen Activity (which should be the Activity at the bottom of the stack for the app)?
Or do you start a new Login Screen Activity and go straight to that?
Or should I call the Login Screen Activity with the FLAG_ACTIVITY_CLEAR_TOP passed through the intent?
After the user is able to login the second time (lets say he got his credentials reinstated), if the Activity is now the only one in the stack, pressing the back button will take him out of the App, as opposed to going back to what he was doing before having his credentials revoked. What is the best approach to this?
My personal choice is to set up all of your activities (besides the login activity, of course) to check the user's credentials in onResume() (I do this by inheritance, with all of my Activities inheriting from an abstract AuthorizedActivity, but do what works for you). If the user does not have credentials (either because they don't have them or they've been revoked) then I startActivityForResult() the login activity. If the LoginActivity returns a valid user, all is well. If the user is a valid but different one from who was logged in before, I take them to my root activity with FLAG_ACTIVITY_CLEAR_TOP. pressing the back button from the loginActivity does them no good, as the app will bounce them straight to home.
I find that the approach gives me flexibility in deciding when and how I will check/revoke credentials, even if they leave (briefly) the application.
Login screen activity with FLAG_ACTIVITY_CLEAR_TOP will work but will remove all of the user's history. Do you care if they renew their credentials and then press back? Should that preserve their history?
You may consider FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_NO_HISTORY for the login activity.
If you want all previous tasks to redirect to the login screen, you'd make sure to do the check in onResume so that they can't go back through the stack after having permission revoked.
Keep in mind that you can also set these flags on the <activity> element in the manifest.

Categories