i'm trying to pass the countdown timer value as textview to next activity, but i don't know how, do i need do use intent.putextra() ?
There is my code:
countDownTimer = new CountDownTimer(5000, 1000) {
#SuppressLint("DefaultLocale")
public void onTick(long millisUntilFinished) {
timpRamas.setText(String.format("%d:%d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
Intent intent = new Intent(c1_1.this,TimpExpirat.class);
startActivity(intent);
finish();
}
}.start();
You must pass the data of the textView to the next activity and in the next activity you can set the data in another view or do whatever with it:
public void onFinish() {
Intent intent = new Intent(c1_1.this,TimpExpirat.class);
intent.putExtra("data" , timpRamas.getText().toString());
startActivity(intent);
finish();
}
In the TimpExpirat activity:
//get the data in onCreate()
Intent intent = getIntent();
String time = intent.getStringExtra("data");
//time now has the time that was last set on your textview, you can set it
//to a new textview or do whatever with it.
Related
I'm looking to store the result of a stopwatch timer when I click the stop button, then place this result in a table on the second page of my an android app. The stopwatch works as planned, but how to I store the result?
mButtonStartPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!running) {
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
mButtonStartPause.setText("Pause");
} else {
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
running = false;
mButtonStartPause.setText("Start");
}
}
});
You'll want to pass your data through the Intent.
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("STOP_WATCH_TIME", "8:03"); // Pass whatever data you want.
startActivity(intent);
And then within your ResultActivity, you can get that data.
String time = getIntent().getStringExtra("STOP_WATCH_TIME");
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");
I want to get the value of a TextView which is defined in another activity? I am using android studio.
You should use intents for passing values to another activities.
in first activity:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("YOURKEY", yourTextViewValue);
startActivity(intent);
Access that intent on next activity
String textViewValue= getIntent().getStringExtra("YOURKEY");
First activity (which has TextView)
SecondActivity.launchActivity(this, textView.getText().toString())
Second Activity (which need the text value)
public static void launchActivity(Context context, String textViewValue) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("textValueKey", textViewValue)
context.startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent() != null) {
String textViewValue = getIntent().getStringExtra("textValueKey");
}
...
}
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?
TL;DR: My intent takes too much time to start from a different thread, whereas starting from the main thread is very fast. I dont actually know if this is a problem with threads or with the onFinish method
So, I have a countdown timer. It counts down from twenty seconds, and onFinish() I have an intent. I am also periodically setting the text of my textView based on millisUntilFinished. I noticed, that after the textView says 1 second left, the intent starts after 3 seconds.
But, if I am switching activities by using an intent from OUTSIDE of the onFinish method the next activity starts quickly. So,
Why does starting an intent from a the onFinish method take longer than usual?
According to my little test with my timer, I decided that I need a better and faster way to start my intent, since clearly, the onFinish method launches after more time then the timer actually starts. So, what should I do to start my intent faster? I need it to be immediate...
public void startTimer() {
timer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) millisUntilFinished/1000;
timetext.setText(seconds + ":00");
}
public void onFinish() {
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
startActivity(intent);
}
}.start();
}
Thanks,
Ruchir
Try placing the startActivity(intent) outside of the timer as such:
public void startTimer() {
timer = new CountDownTimer(20000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) millisUntilFinished/1000;
timetext.setText(seconds + ":00");
}
public void onFinish() {
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
}
}.start();
startActivity(intent);
}
You can have a try :
public void onFinish() {
cancel(); //Cancel the countdown
Intent intent = new Intent(MainActivity.this, GameOver.class);
intent.putExtra("score", score); // pass your values and retrieve them in the other Activity using keyName
intent.putExtra("classname", "com.example.ruchir.swapproperties.MainActivity");
startActivity(intent);
}
`