I am building an Android app. How can I get the calculations' solutions to be displayed on a new activity?
Make your calculation in Activity A.
And send them to the other activity using intent.putExtra(...)
Take a look in this guide.
On activity A
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("YourKeyWord", yourVariableWithValue);
startAcvitity(intent);
On activity B
int result;
Bundle extras = getIntent.getExtras();
if (extras =! null) {
result = extras.getInt("YourKeyWord");
}
Activity A:
public class First extends Activity{
int a,b,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a = 1,b = 2;
c = a + b;
Intent intent = new Intent(this,Second.class);
intent.putExtra("result", c);
startActivity(intent);
}
}
Activity B:
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int result = intent.getExtras().getInt("result");
}
}
Related
I am new to learning Array in android studio. Please show me some examples in details. I have write an example here and I want to display the Array data from MainActivity into second_page activity .
MainActivity.java
public class MainActivity extends AppCompatActivity {
String my_array[]={"dog","cat","tiger"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void next_page(View view){
Intent intent = new Intent(this,second_page.class);
intent.putExtra("my_array_next", my_array);
startActivity(intent);
}
}
second_page.java
public class second_page extends MainActivity {
TextView get_data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
get_data=(TextView)findViewById(R.id.tv);
Intent intent=getIntent();
// coding here to display the array data
// sth like abc.setText(display_array_data);
}
Please advice. Thank you!
If you are trying to send a String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
String[] my_array = new String[]{"dog","cat","tiger"};
intent.putExtra("myArr", my_array);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("myArr");
}
this may helps you
In second_page.java, receive the array you pass via Intent and set it to your TextView like this
#Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] array = intent.getStringArrayExtra("my_array_next");
// TextView display a String so you should convert your Array to String
String str1 = Arrays.toString(array);
get_data.setText(str1);
}
First take the array:
Intent intent = getIntent();
List array;
if (intent.getExtras() != null) {
array= intent.getExtras().getBoolean("my_array_next");
}
Then print
get_data.setText(array.toString());
Sending Class:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
Reciving Class:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}
In your Second activity:
String[] array=getIntent().getStringArrayExtra("my_array_next");
I think you need to go through basics, go to https://developer.android.com/index.html to get started.
I would like to dynamically start an activity based on the previous activity's input. I have input a string through the previous activity, the only thing is this specific code throws the error
cannot resolve constructor 'Intent(com.MentalMathWorkout.EasyCountDown, java.lang.String)'
Is there a way to make this work?
public class EasyCountDown extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ecd);
Intent intent = getIntent();
String test = intent.getStringExtra(MainActivity.TEST_TYPE);
String cstring = ".class";
final String activity = test.concat(cstring);
Intent intent = new Intent(EasyCountDown.this, activity);
startActivity(intent); //Start test
}
The ComponentName object does just that:
String activity = intent.getStringExtra(MainActivity.TEST_TYPE);
Intent intent = new Intent(this, new ComponentName(this, activity));
startActivity(intent);
That's assuming this is an instance of Activity. (for a Fragment, use getActivity(), obv.)
I have a class on here:
com.yasinkacmaz.newproject.activity.ProfileActivity
My test string like that:
"com.yasinkacmaz.newproject.activity.ProfileActivity"
And it working good:
public class EasyCountDown extends AppCompatActivity {
final Activity thisActivity = this;
private Intent previousIntent,nextIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
previousIntent = getIntent();
String test = previousIntent.getStringExtra(MainActivity.TEST_TYPE);
final String activity = test;
Class newclass = null;
try {
newclass = Class.forName(activity);
} catch (ClassNotFoundException c) {
c.printStackTrace();
}
if(newclasss != null) {
nextIntent = new Intent(thisActivity, newclass);
startActivity(nextIntent);
} else {
Toast.makeText(getApplicationContext(),"new class null",Toast.LENGTH_SHORT).show();
}
}
}
Dont forget you can use switch case or etc., because in this way you can get ClassNotFoundException and your intent will be null.
I have this activity which is called MainPutShipActivity and I want to start it again so it will do the same thing but it doesn't even enter the OnCreate method.
here is the MainPutShipActivity:
public class MainPutShipActivity extends Activity implements OnClickListener{
private static final int MAX = 10;
private String name1,name2;
private Player plr = new Player();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_put_ship);
Intent intent = getIntent();
plr = (Player) intent.getSerializableExtra("player");
name1 = plr.getName1();
name2 = plr.getName2();
}
public void finished() {
Intent in;
}
if (plr.isTreated() == false) {
plr.setArr1(arr);
plr.setShip1(ships);
this.finish();
in = new Intent(this,MainPutShipActivity.class);
in.putExtra("player", this.plr);
}
else {
plr.setArr2(arr);
plr.setShip2(ships);
in = new Intent(this, MainGameActivity.class);
in.putExtra("player", this.plr);
}
plr.setTreated(true);
this.finish();
startActivity(in);
}
When i enter the finished() procedure for the first time it suppose to start the MainPutShipActivity again but when it starts the activity, it skips the onCreate and goes straight to the finished() method for some reason.
I would be very glad for any kind of help.
Pay attention to the key passed in the intent
Instead of :
plr = (Player) i.getSerializableExtra("plr");
set
plr = (Player) i.getSerializableExtra("player");
because you are setting
in.putExtra("player", this.plr);
I'm trying to send a madeObject from Start_Activity to Next_Activity after finishing Start_Activity.
How to make a code in Start_Activity and Next_Activity?
My sequence is :
1) Start_Activity.onCreate()
2) Start_Activity.makeObjectData()
3) Start_Activity.putextras(madeObject)
3) Start_Activity.startActivity() : start Next_Activity.
4) Start_Activity.finish() : finish Start_Activity
5) Next_Activity.getExtras()
Here is Start_Activity
public class Start_Activity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_launching_activity);
:
//// makeDataObject ///
:
Intent intent = new Intent(getApplicationContext(), Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
}
This is Next_Activity
public class Next_Activity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_launching_activity);
??? <-- How to get the madeDataObject of Start_Activity?
}
}
bundle.putSerializable takes two parameters. The first is a String. It has to be unique for this bundle object. You will use it afterwards, to retrieve your object.
bundle.putSerializable("my_unique_key", madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
on Next_Activity, you can retrieve the intent with getIntent()
Intent intent = getIntent();
if (intent.getExtras() != null) {
intent.getExtras().getSerializable("my_unique_key");
}
Of course the object you are trying to pass from one activity to another has to implements Serializable
send extras to next activity as:
Intent intent = new Intent(getApplicationContext(), Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("myclass", madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
Get it in next activity like:
Intent intent = getIntent();
if (intent.getExtras() != null) {
intent.getExtras().getSerializable("myclass");
}
Activity myActivity = AssumeSomeActivityExists();
Intent openActivity = new Intent();
openActivity.setAction(Intent.ACTION_VIEW);
openActivity.setClass(myActivity,B.class);
myActivity.startActivity(openActivity);
When we do something like above how to make B instance know that it is been called and created by Activity myActivity?
Use extras with your Intent.
Smth like openActivity.putExtra("calledFromA", true)
Then in B:
protected void onCreate(Bundle savedInstanceState) { {
super.onCreate(savedInstanceState);
boolean isCalledFromA = getIntent().getBooleanExtra("calledFromA", false);
}