I am building a chat client and chatting with every user opens same activity ChatActivity passing an username as intent extras like this one.
Intent.putExtra("user", username);
Where username being the one one you are chatting with. After extras are sent, ChatAcitivity opens up, handles the extra's username and work accordingly. Now, if i am chatting with multiple users at once, say user1 and user2, each time the activity restarts to handle the data. How can i preserve the chat with both the user and open up the already initialized activity so as to reduce load both to the server & user.
I want something to identify the intent that needs to be opened like chatting with user1 brings user1 ChatActivity to the front and so i can use:
resultIntent.setFlags(Intent.FLAG_ACTIVITY_USER1ACTIVITY_BROUGHT_TO_FRONT_);
or something to call that up.
How can i do that??
I understand you want to have 'duplicate' instances of the ChatActivity, one for each user. You can use the FLAG_ACTIVITY_NEW_DOCUMENT flag, the document being a conversation with a specific user :
// the URI is used as a document ID
// a stack / activity with the same id will be reused
// and the intent passed to onNewIntent()
// otherwise a new activity is created
startIntent.setData(Uri.parse("http://my.domain.net/users/" + username));
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
Please note that each conversation will have its own entry in the Recents screen (Overview screen). This may or may not be what you want.
Related
I'm new to android studio and developing apps.
I currently have a list , in my case of people of type ViewList.
I want to make the app display information of these people whenever the user clicks on him.
So for example, if a user clicks Elon Musk, the app will take the user to a page that will display information about Elon musk. The same with other people.
How can I program this without needing to re create a new activity for every person on the list?
TIA
To do this you would need to pass the data through to another activity with an intent, and changing the data in a activity(e.g. displayPerson.class)
Now there is multiple ways of doing this but generally is close to the same concept.
note: Where is the data coming from, is it from firebase or is it hard coded into the app?
You can create one activity and show detailed information about the clicked user in that activity.
You don't need to create activity for each user. Just one activity is enough.
user.setOnClickListener(v -> {
Intent intent = new Intent(CurrentClass.this, UserDeatilClass.class);
intent.putExtra("userName", userName);
intent.putExtra("userAge", userAge);
intent.putExtra("userEmail", userEmail);
startActivity(intent);
});
In the user Detail class
protected void onCreate(...) {
userNameTV.setText(getIntent().getStringExtra("userName"));
userageTV.setText(getIntent().getStringExtra("userage"));
userEmailTV.setText(getIntent().getStringExtra("userEmail"));
}
Also, if you are using Database you can just send only the ID number of the user, and fetch all data about the user from database, finally you can display the whole information about that user from database.
But if you don't want to create an activity, you can create a custom Alert Dialog and display the detailed information of the user in the Alert Dialog.
I was reading and I can['t get to an answer, I want to send a push notification to my user, but when the user clicks open a custom intent, I don't want to do this for all the notifications, just for a couple. Let's say I want to send here
The value to open a custom intent, and then if I don't put anything there, the app will still open the default intent, I need this because I want to send the user sometimes to another activity
One of the way to achieve this is to always open the MainActivity and pass the information in your notification data(called as payload schema) to the MainActivity as an intent. In your payload schema you will pass in the information which will help you decide which activity to open. These information will be in form of key value pair. For example you can define a parameter action and this will contain an identifier for the activity that you want to open when the notification is clicked.
In your main activity you will have a logic to read this information from your notification schema and redirect it to appropriate activity. Something like this
//Main activity code
public void onResume() {
super.onResume();
if(action == "Activity1") {
//start activity 1;
}
}
If you dont pass anything in notification it will by default launch the main activity.
I'm making a registration form with 5 steps (5 activities, Similar to facebook signup form).
The 1st activity is to put email
The 2nd activity is to put name and so on. After the user complete all steps, i validate the field email (to avoid being duplicated) from the server side. What i want is, if the user email exist, i want to go back to 1st activity so the user can enter other email and the submit the form again to the server. Whats the best way to do that?
Thanks
In your LastActivity after some action.
Intent intent = new Intent(this,FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent );
This flag clear recent Activity in the activities stack.
so I have an app where users have accounts/profiles, when say you type name and surname in search it would bring to activity called ProfileViewer and display that users details.
In PHP I would have link like:
John Sample
So when user clicks on that - it would go to PHP page called profile.php and load data from MySQL database WHERE userid=xxx.
So what's the alternative in Android? I am a bit lost, what are some of my options on how to identify which userID is Activity loading?
Could I use putExtra("userId", xxx) and then use getExtra()?
In other words: How can I make one activity act like page which accepts 1 param and then uses it on opening?
Intent extras are generally the way to go in these cases.
From the first activity you would:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("userId", 1);
startActivity(intent);
And you then retrive the information in the second activity like this:
getIntent().getIntExtra("userId");
Any one can tell me how could i get inbuild notification listner in android. What all i want is if user put password on screen and if then some notification arrives. and if user click on notification, I want password field should be reset.
See http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification to create a local notification.
In the intent you use for the notification add an extra bit of data that you define (e.g. "ClearPassword" as a boolean of true).
In your activity, check for extras and your specific extra and if it is set then you can clear the field.
Why don't you just clear the password field in the onPause method of the activity? If the user clicks on the notification, the pending intent will launch a new activity (most in the cases from another application). When your activity is no longer the foreground activity, its onPause method will be called.