Android: new Intent() starts new instance with android:launchMode="singleTop"
i got single top to work as per the link above, but I am having a hard time putting "extras" in the intent and then performing a function on my original activity.. is this possible?
Intent I= new Intent(context, away.class);
I.putExtra("number", number);
I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
this snippet is from my broadcast receiver and it refers back to my main class.. in my main class my code is like so..
Intent I = getIntent();
int number = I.getIntExtra("number", -1);
so my question is the following..
how can i get my main activity to evaluate the number i send back and then fire a function when my receiver class fires it?
You have to override onNewIntent and get the extra there.
You have to override onNewIntent and get the extra there.
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
int number = intent.getIntExtra("number", -1);
}
In your broadcast receiver
Intent I = new Intent(context, away.class);
I.putExtra("number", number);
Log.d("here", "number = " + number);
I.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(I);
Related
In my app, there are so many activities which are referred to as levels. And one activity is Reward activity. when i win level-1, reward activity opens. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.
Houselevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
startActivity(intent);
}
if(activity.equals("level1")){
Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
startActivity(intent);
}
}
});
}
With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. Because of this, the build will never be successful.
Also, when you declare intents, you MUST pass on the activity_name.class file.
Something you can try:
1) HouseLevel1.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level1");
startActivity(intent);
}
}
2) HouseLevel2.java
public void getReward(){
if(count == 3) {
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
intent.putExtra("activity", "level2");
startActivity(intent);
}
}
3) Reward.java
public void replayLevel() {
replay = (ImageButton) findViewById(R.id.replay);
Intent intent= getIntent();
activity = intent.getStringExtra("activity");
replay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View paramView) {
if(activity.equals("level2")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class);
startActivity(intent);
}
else if(activity.equals("level1")){
Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class);
startActivity(intent);
}
}
});
}
Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually.
Here's a small article that might be able to help you with the problem
http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity
If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity.
What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity.
Your app will go back to the Activity1 or 2 whoever call it. On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK.
Then you have whatever was set on the Reward activity. The Reward activity don't need to know from where it came from if only will grab some data. So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity.
It is explain here check the accepted answer.
How to manage `startActivityForResult` on Android?
So i got two activities, a main then a secondary, we'll call them activity_A (main) and activity B( the second ).
So my activity_A is getting some data, then sending it to activity_B with this code :
Intent i = new Intent(this,activity_B.class);
i.putExtra("Charge_Batterie", bat);
startActivity(i);
activity_B receive it correctly with this code :
String bat = getIntent().getStringExtra("Charge_Batterie");
My problem is that my activity_A is refreashing it's value "bat", so when i send it with intent it doesn't refreash in activity_B.
So i'm wondering, is my activity_A sleeping ? If yes how can i make it stay alive ?
I´m doing the same thing and it works for a long time - activity A runs a timer and launches activity B with extras.
When I want to refresh activity B with "new extras", I´m doing this:
// check if activity B is created and running:
if (intent == null) {
System.out.println("Creating Activity - Intent is null");
intent = new Intent(context, ActivityB.class);
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}else {
System.out.println("Reloading Activity - Intent exists");
finish();
intent = new Intent(context, ActivityB.class);
intent.removeExtra("playlistsList");
intent.putExtras(ImagedownloaderActivityBundle);
startActivity(intent);
}
As far as I know, this isn´t the best way to achieve the background task - it should be running on a service, perhaps doing the same when one needs to refresh.
I have question about passing data between activities. Is possible from main activity open few next activities, and return result from last opened to root activity and how? I've added screenshot for better vision what I mean. Thanks for answers.
enter image description here
working with this question #Sahil Lombar
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("data", "data");
startActivity(i);
in other activity we can
//firt validate if value extra "data" it was sent
if(getIntent().hashExtra("data") {
String value = getIntent().getStringExtra("data");
Log.d("SecondActivity", value);
}
working with String, int, boolean, double, Byte and array of these
more.. https://developer.android.com/reference/android/content/Intent.html
Start the First Activity with Intent holding the result;
In Activity 3
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("data", "data");
startActivity(i);
finish();
In Activity 1 receive the result in public void onNewIntent or if you are starting a new activity in the onCreate method
public void onCreate(){
super.onCreate();
Intent i = getIntent();
String result = i.getStringExtra("data");
}
As the step value of a variable of an Activity for a TextView other Activity? Can Help?
Thank you!
((See explanation on the picture))
Intent activityTwo = new Intent(this, Activity2.class);
activityTwo.putIntExtra("key", sumSettlement);
startActivity(activityTwo);
Now, in Activity2:
if(getIntent() != null) {
textView.setText(String.valueOf(getIntent.getExtra("key"));
}
You can use of local broadcast receiver.
First register receiver in Activity B
//in onCreate Method
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("my-event-name"));
// It will be called whenever an Intent
// with an action named "my-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
// show this message in textview
}
};
In Activity A
//broadcast this
Intent intent = new Intent("my-event-name");
intent.putExtra("message", Integer(sumSettlment).toString());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
the easiest way is to use Intent: in first Activity
Intent intent =new Intent(CurrentClass.this,DisClass.class);
intent.putExtra("myTextValue",textView.getText().toString());
startActivity(intent);
in the dist activity do the following :
String myValue=getIntent().getExtra().getString("myTextValue");
textView.setText(myValue);
This question already has answers here:
Android: Clear the back stack
(39 answers)
Android: Remove all the previous activities from the back stack
(18 answers)
Closed 9 years ago.
For example,
I have activity A, B, C, D
A call B
Intent intent = new Intent(A,B.class);
startActivity(intent);
Then, B call C
Intent intent = new Intent(B,C.class);
startActivity(intent);
After that, C call D
Intent intent = new Intent(C,D.class);
startActivity(intent);
In Activity D, I call finish(). It will return back to Activity C.
My question is how can I clear Activity A, B, C before calling finish() so that the app quit like normal.
Don't suggest call finish() on every startactivity because the app can press back to previous activity to continue.
This should work definitely...
Intent intent = new Intent(D,A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("close",true);
startActivity(intent);
and in oncreat of A activity u have to write
if (getIntent().getBooleanExtra("close", false)) {finish();
}
else {
{
//ur previous code here
}
Have fun if any problem u can ask
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_SINGLE_TOP
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK
which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Activity A -> Activity B and then you want to get back to A from B, but the extra flags shouldn't affect your case above).
Try adding FLAG_ACTIVITY_NEW_TASK.
So your code would be:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am using the following in my application. Hope it will help.
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear the stacks
intent.putExtra("exitme", true); // tell Activity A to exit right away
startActivity(intent);
and in Activity A add the following:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
if( getIntent().getBooleanExtra("exitme", false)){
finish();
return;
}
}
try with Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
see here
http://developer.android.com/reference/android/content/Intent.html