Closing the background activity which has the same name - android

I'm trying to develop an app which has two editText fields for entering location. So I made two java files
1.MapsActivity.java(with activity_maps.xml)
2.AutoComplete.java(with autocomplete.xml)
In MapsActivity while clicking the 'from' text field, it will open the AutoComplete activity and after clicking any of the suggestions in it will open a fresh MapsActivity and places the clicked item . Likewise for 'to' text field also.
I am using Intents to pass the clicked values. So at last, five activities are opened when the two text boxes are filled. When I click back button ,it is showing the auto complete activity used the previous time.
Question:
Is any other way to close a particular activity with same name running behind?
Is there any other way to pass values by super.backpressed(); ?
enter image description here

You can try using startActivityForResult() method. That way you can use the result from the second activity in the first and also you wouldn't need to start a new activity. You can read up on it here startActivityForResult

try to clear backstack/clear_top/single_top using the intent.
Just add/set flags in intent.
or
finish activity after startActivity.
or
you can override the onBackPressed method to call the specific activity.

Related

Set and load a certain text

I'm currently programming an app where you should enter a text which is stored. The text should be able to be loaded in the app (maybe another activity) then.
It should be able to be used this way:
task.execute(new String[]{city,lang});
Is this makeable?
Thanks
You can use an Edittext on your first Activity on which user can type something.You can get that "something" programmatically in the same activity as a string.Now if you want to avail that string to another activity then you can use an Intent from the first activity to the second activity which will avail that string in the second activity.

launching already launched activity from notification creating problematic activity

i have 3 activities A-B-C .Lets say i am on activity B and from a listview there i launch activity C now if a notification comes which has an intent of launching activity C.
So the problem is I am getting multiple instances of activity C
When i use launchMode singleTop or instance or task the problem of multiple activity instance is solved but the newly launced activity does not work properely as it is desired to be.
Please help me out tired of using flags and stuff but not able to overcome this problem.
The scenario is Just like whatsapp , if u r talking to person one and a message of person 2 come as notification ,when u click on that notification the activity relaunches and works properely. This is exactly what i want. Please help me out how to achieve this . :(
Thanxx in advance
What Flags did you try and what exactly is not working, means, how does the Activity behave?
What you describe with WhatsApp could be achieved with two steps:
Use the FLAG_ACTIVITY_SINGLE_TOP for the Activity.
Overwrite the Actvity.onNewIntent(Intent intent) method in the Activity. This method is called instead of creating a new Activity and gives you the new Intent. I assume that the Intent contains all necessary information in the extras.
The user experience should be as follows:
User chooses from the list, which opens the new Activity
He/she can press home or back button from here. This brings the home screen or the list back.
If, for any reason, somebody else calls startActivity for your Activity while it is running, the onNewIntent is called. There you can add code to refresh the content, maybe have an Alert that tells the user that the content has changed, and shows all that to the user.
If the user presses back or home now, he/she will get to the list or home screen
If that is, what you're looking for, then the flag and the method is what you need

how to implement calculator on edittext?

In my android apps,i want to press edittext the follwing popup calculator shown.when i enter calculation and press "ok" button result will stored in edittext.how to do this?give me any example code?
thanks in advance..
Make an Activity that does all the Calculator functionality you need (I'll leave that as an excercise.. :P)
In your original Activity, on the click even of your EditText, call the Calculator Activity using startActivityForResult()
The Calculator Acitivity should use setResult(RESULT_SUCESS, simple_intent_with_result_as_extra)
When the Calculator Activity Exits, the onActivityResult will be called on your original Activity with that Intent you passed earlier.
Extract the result from the extras and display it in that EditText.

Open the first activity on the current tab by clicking same tab

Is it possible to open any acticity on the current tab by clicking the current tab itself?
What is to be done in my application:
user performs a search , a listactivity is displayed on same tab(Using TabGroup activity). Then If User presses the same tab, again the search page should open.
Is this possible?
Try this,,,,
Rather than starting two activities, you perform both in same activity.
On pressing tab show search activity.
show search screen, when user clicks search, get your search result and set it as current activity view i.e, setContentView(list);
when user clicks search again start SearchActivity again (as usual).
Refer this link:
Launching activities within a tab in Android
In first answer,you can set SharedPreference Variable and then check for that variable to know which activity to be loaded in this tab(in YourActivityGROUP class).you can set Extras to Intent accordingly in your main activity which you use to open an activity in a tab. (I haven't tried this,but i think,this will solve your problem.)

Return list item from search dialog to calling activity

My application shall work the following way:
Activity #1 contains a text field (EditText) and a button.
If the user clicks the button a search dialog is opened via onSearchRequested().
This calls the searchable activity #2 which extends ListActivity. It provides a list of items via setListAdapter().
If the user clicks on a list item activity #2 shall pass the selected item's text back to activity #1 and display it in the text field.
Bullets #1-3 are clear and working. However I don't have any idea how to implement #4. I know about the possibility to use intents but it doesn't work if I use an intent after onSearchRequested().
Thanks,
Robert
The following solution is working fine for me:
http://blog.dpdearing.com/2011/05/getting-android-to-call-onactivityresult-after-onsearchrequested/
I would simply send an intent with your selected item as extra (putExtra) to your activity#1 (since the search dialog is between activity#1 and activity#2, you can not use startActivityForResult to post back the result to activity#1)
If the search dialog is in activity#1, then you can use startActivityOnResult (thanks dmon)
Simple, quick fix: store the data statically and do a check to retrieve it in Activity #1's onResume().

Categories

Resources