There is a code that sends an int 'a' from the main activity to activity B. It also starts activity B with the fade animations. However, this code creates 2 of the same activity B's and I only need 1 activity B. How can I fix this so that it only makes 1.
new Handler().postDelayed(new Runnable() {
public void run() {
Handler splash = new Handler();
int a = 1;
Intent myIntent = new Intent(MainActivity.this, Differentiate.class);
startActivity(new Intent(MainActivity.this, Differentiate.class));
myIntent.putExtra("HEADER", a);
overridePendingTransition(R.anim.fade_in_switch_fast,R.anim.fade_out_switch_fast);
startActivity(myIntent);
finish();
}
}, secondsDelayed * 2000);
You are starting the second activity twice. Remove the following line from your code and move overridePendingTransition after you use the intent to start the activity:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Hey just remove this line from code:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Rest is fine in your code.
That's because you are starting Activity B twice!
you should remove this part of your code:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Related
I have code that passes an arraylist to another intent, how can I return the value of updated arraylist back to the main intent?
Arraylist in main intent code:
ArrayList<Users> vuser = new ArrayList<>();
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
intent.putParcelableArrayListExtra("vuser", vuser);
startActivity(intent);
Do I have to pass the arraylist again to the main intent? Like this:
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.putParcelableArrayListExtra("vuser", vuser);
startActivity(intent);
You can pass it using the getIntent() method. When you go back, you can override the onBackPressed() method and add your own login like the one given below. Check the code out:
#Override
public void onBackPressed(){
getIntent().putParcelableArrayListExtra("vuser", vuser);
finish();
}
This will send data to the previous activity and then finish the current activity.
I'm trying to send a result from my MainActivity to SecondActivity, but it always returns a default value, not the result. I'm a begginer, if there is someone that could help me it would be nice.
I've tried everything that came to my mind, but nothing worked.
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String prvi=etPrviBroj.getText().toString().trim();
String drugi=etDrugiBroj.getText().toString().trim();
prviBroj=Integer.parseInt(prvi);
drugiBroj=Integer.parseInt(drugi);
rez=prviBroj+drugiBroj; //declared as an int and set to 0
Intent intent=new
Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
}
});
//and in the second activity
rezultat=getIntent().getIntExtra("rez",0);
in first activity :
btnSum.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!edt1.getText().toString().isEmpty() && !edt2.getText().toString().isEmpty()) {
sum = Integer.parseInt(edt1.getText().toString()) + Integer.parseInt(edt2.getText().toString());
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
intent.putExtra("RESULT", sum);
startActivity(intent);
txtAns.setText("" + sum);
} else if (edt1.getText().toString().isEmpty()) {
edt1.setError("Please enter no1 ");
} else if (edt2.getText().toString().isEmpty()) {
edt2.setError("Please enter no2 ");
}
}
});
second activity
int sum= getIntent().getIntExtra("RESULT",0);
You can store your sum of two variables in one int variable. When you click of your second button make instant of Intent and call startactivity at that time.
You're creating an Intent just to set the variable you want to pass to the second activity but in your code this Intent is not linked to the new Activity.
If you want to pass a value between two activities with this method, you have to start the second activity with the Intent you gave the extra value to.
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
startActivity(intent);
If you want to start the second activity with another Intent you have to pass the value in another way.
Intent intent=new
Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("rez",rez);
startActivity(intent);
In SecondActivity
Intent intent = getIntent();
String abc = intent.getStringExtra("rez");
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?
I used intent to send a primarykey of my app to another activity but it debug my application I don't know why?
I'm sure that I put the syntax corrrectly and I tried many way I used bundle object but the same result the application has stopped
Activity iden_espvol
Intent intent=new Intent(iden_espvol.this,verf_tel_espvol.class);
intent.putExtra("TEL",tel.getText().toString());
startActivity(intent);
finish();
Activity verf_tel_espvol
Intent intent = getIntent();
String sent=intent.getStringExtra("TEL");
Try this code.. and i hope you define both activity in manifest file..
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(LayoutActivity.this,MainActivity.class);
intent.putExtra("TEL","65421321");
startActivity(intent);
finish();
}
});
and in second activity onCreate() method
Intent intent = getIntent();
String sent=intent.getStringExtra("TEL");
Log.d("Data",sent);
make sure value get only oncreate or onResume method because first activity finished that time call second activity this method.
I have a MainActivity class with 3 Fragments (each in their own class)
My third fragment (LoginFragment) will allow Login a user and then go to a new activity (new Intent) with some info for that user like the product.
If I press back on that Intent will go back to the LoginFragment.
I override the #OnBackPressed to start the MainActivity:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
I need to know how to replace that Fragment with the LauncherFragment (Fragment 1) in MainActivity.
I have this solution but it takes 0.5 sec to 1-2 sec based on device
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
this.finish();
would be cool to go direct to Fragment 1 like to finish the third fragment thanks :)
I fixed the issue in this idea
onBackPressed() I call a new Intent but with extras
In the MainActivity that has the 3 fragments onRestart() I check if it coming from this class ( has that extras ) than go to this fragment (click,replace,delete)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Constants.Intents.NAVIGATE_BACK, true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
}
on the MainActivity I got this
#Override
protected void onRestart() {
super.onRestart();
Intent intent = getIntent();
boolean navigate = intent.getBooleanExtra(Constants.Intents.NAVIGATE_BACK, false);
if (navigate) {
View homeView = bottomNavigationView.findViewById(R.id.home);
homeView.performClick();
intent.removeExtra(Constants.Intents.NAVIGATE_BACK);
}
}
If you want it to be as fast as possible, use one activity and fragments to vary the contents. Adding a fragment doesn't create a new window, whereas an activity does.
Also look at your application logic in fragment / activity startup (onCreate(), onResume(), etc). That's going to be the main factor.