How to open an activity twice but with different content? - java

I'm developing a chat app, but I have a problem.
I've a list with the contacts and when I select one contact I'm starting a new activity
Intent i = new Intent(this, MessageScreen.class);
startActivity(i);
but, when I choose another contact to talk I will use the same activity.
but it always open with the last contact screen and the variables still with the old values.
I would like to make something similar to google talk, where you can start to talk with another contact, and all the messages use the same screen, and you can change between the chats fast with no need to reconstruct the screen, reload the messages, etc..
Anyone have any idea of how to implement this?

Sliding between activities isn't a common feature, it sounds like there is one second activity that has a ViewPager that is populated with multiple chats. When starting this activity, they're probably adding the Reorder to front flag to the intent and have overridden onNewIntent to add a new view to the pager.

Try something like*:
i.PutExtra ("key", value);
before starting the activity (e.g. store the user name) and then read your value from the activity and adjust (e.g. UI) it based on the value
note: syntax could differ a bit since I do my Android stuff from C#

Related

Skip the activities in between and reach the specific activity in android

I have following app structure
MainFragmentActivity
Notification Fragment
MainFragment
ChatFragment
So on etc.
I also have set a broadcast on all "Activities" That will show a popup if any Push notification is arrived.
The popup has two options, View or Ok.
Now let's say i am on ChatFragment , from there i go to Chat Contacts List ( Activity) Then on a ChatScreen(Activity) then on ProfileScreen(Activity) of User
The Stack of Activities will be like
MainFragmentActvity -> ChatContactsList -> ChatScreen -> ProfileScreen
What i want is, when i click on "View" on ProfileScreen it should remove all stack activities and take back to MainFragmentActivity and replace its Current Fragment with NotificationFragment.
I can do finishAffinity() Restart the app and put MainFragment but i want to support 16- APIS to.
Flag Clear Top will remove MainActivity and i do not want to restart it because then it has to call a lot of apis
I do not want to put NO History FLAG because i need to put All other activities in History.
What can i do here .
Also i do not want to put startActivityForResult it would be so complex.
Any ideas would be appreciated
What I understood from your explaination is, you want to create wizard like scenario to control the activities flow at run time. (Wizard means, for example, installation window(wizard) of application on microsoft windows)
For that, there is already developed library by Nimrod Dayan,
called WizarDroid.
Here is the link for home page of the same, WizarDroid
Try it out, its good.
or
You can use view pager (WizarDroid is also built on view pager)

Android: How can I exchange data between multiple still running activities directly?

I need a nice way to exchange data between two activities directly. I have one same custom title for all my running activities and in my first activity I display the GPS state in that title with an image (found/still searching). The LocationListener is in my first activity and if the GPS state changes, I would like to update all the titles of my running/displayed activities. At the moment I can only change the title of my first activity.
I know that I can exchange the data through the SharedPreferences and by Intents which passes the data as Extras but as far as I know, I can only receive the extras in the onCreate Methods of my other activities.
What I want do have is, that the data is updated on a still running activity (onCreate is passed).
I hope you understand what I mean :)
Example scenario:
I start up my App. The GPS localisation is running (first Activity). Meanwhile I navigate to another activity of my App. Now, If the GPS state changes I need to update the title of my second activity which is shown at the moment.
Is there any way to solve this problem?
Thanks
Apart from creating an activity with two fragments, there is a tricky way that serves for other scenarios :
Let's say, if you have an variable (let the text in the title bar is an variable) that needs to keep update, while it's impossible to use broadcast (message missing is not allowed or activity is killed), you can use the text saving from shared preference, and extract it every single time.
For example, Activity A uses String from sharepreference, key is "title", and default value is your app name. After B, C or D updates the title text, A also needs to update. So you just update the value of key "title" in share preference. After A is revoked, it will grasp the title in sharedPreference again and update the title accordingly.
Hope this help. :-)

stack activity in android

Hi i am working with android , and i have a problem with the activity stack. As i know, when someone uses the back button, reload the back activity. But in the case i have many layouts shown from one activity, how can i go back to them.
Here is the deal, i am using a listview filled with categories, and when i press an item, i reuse the activity and the layout, to show its subcategories. So what i need is to came back no to the back activity, not to the back layout, but to the back "state".
Well, the idea is simple, first i show all the categories with no parent, then when i pressed an item, i show its subcategories.
The easiest way is creating two Activities - for categories and for subcategories. If you try to implement all the logic in a single Activity you won't earn nothing and just end up totally confused. Using Activities simplifies things a lot just because it handles problems such as yours. Hope this helps.
Check out Fragments, they are the stepping stone between a view and an activity. An activity can have multiple fragments and will manage their back stack (if you tell it to).
http://developer.android.com/guide/components/fragments.html
You'll have to use the support library to used them on pre honeycomb devices.

Android moving 'windows'

I'm new to android and I'm need a little help understanding how to move from one window to another. I know i use setContentView(R.layout.main) to load an xml layout file, but how do i swap to another layout file? I assume i would use an onClick method on a button and change setContentView(R.layout.other_layout), but would doing all this inside my main activity make my code cluttered? I could end up having 10000+ LOC easily. Can someone explain the correct way to do this please. Thanks
Intents allow us to call another activity from our present activity. For example our current activity is Act1 and we want to move to another activity, Act2. this can be done as:
Intent i = new Intent(Act1.this, Act2.class);
startactivity(i);
Refer http://developer.android.com/guide/topics/intents/intents-filters.html for more information on intents and activity.
Another option is to call setContentView() 2nd time to change the layout.
You use Intents to launch other Activities.
In your current Activity (i.e. window), you can do the following code to launch a new Activity
Intent i = new Intent(this, NewActivityName.class);
startActivity(i);
You should create Activities. Activity is equivalent to window/frame concept on desktop. Each activity should have a goal towards user interaction, ie. taking inputs and showing output. In your case create two activies, and both of them should have their own layour XML and a call to setContentView() within onCreate().
On button click use startActivity() to invoke a new one. Keep in mind that these activities are stacked top of one another.
A visible screen in Android is represented by an Activity. So instead of loading a different layout file into the same activity, you simple create a new activity with it's own layout and java file.
In order to call this second activity from the first one, or to communicate between activities in general android uses so called Intents.
Just look that chapter up in Android's Dev-Gui.

Re-load Android activity data

I am writing an Android app, part of which will be a survey involving multiple pages of checkbox question and answers. I have created an activity to display the question and options (from the DB) and what I want to do now is when i press the "Next" button it should just reload the current activity with the next question set from the database.
(the activity starts with survey.getNextQuestion() - so its just a case of refreshing the activity so it updates)
Im sure this is a simple thing to do -any ideas?
Thanks
Typically you would start a new instance of the activity for the new question. That way if the user hits back, it has the logical behavior of taking them back a page.
It is possible to update the same activity, but it is more complicated.
To open a new activity:
Intent intent = new Intent(this, MyActivity.class);
intent.putExtra("nextQuestion", x);
startActivity(intent);
Then in your onCreate method, you can pull the next question from the database.
Im sure this is a simple thing to do -any ideas?
It is pretty straight forward, yes. Mayra's answer is perhaps the better one, however here is an approach that will achieve the functionality you specified.
You can use findViewByID(int) to identify the View objects in your layout that need to be updated, and assign the result to an attribute in your onCreate to allow you to access it later.
E.g.
aView = (View) findViewById(R.id.aview);
Your survey.getNextQuestion() can obviously be used to get the next question.
The question can then be placed into the UI by manipulating the Views you obtained in onCreate.
E.g.
aView.setText("your question/answer");
Depending on the number of answers you may need to programatically create/remove checkbox Views from your layout
E.g.
ViewGroup yourViewGroup = findViewById(R.id.yourviewgroup);
View yourView = new View();
//Configure yourView how you want
yourViewGroup.addView(yourView);
youViewGroup.removeView(yourView);
All of this functionality can be contained in a function that is called by the onCreate method and when the next button is pressed.
Don't forget to store the result of the previous question before refreshing ;)

Categories