I have one tabGroupActivity. in this tabGroupActivity I start a new activity called "pideInformacion", in this way:
Intent intent = new Intent(getParent(), pideInformacion.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("pideInformacion", intent);
Then, from pideInformacion class the app open a new activity called verMapaGps:
Intent intent = new Intent(this, verMapaGps.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("verMapaGps", intent);
I would like that in verMapaGps set string value in order to return to pideInformacion class.
I see this way to access to parent activity, but I don't know how can I access to parent activity values to set it.
Activity pideInfoActivity = (pideInformacion) getParent();
I can't be able to use startActivityForResult because If I use this method, the tabhost hides and I want to tabbar is show in whole application.
Thanks in advance!
Try to make the string as a static variable and use it in the parent activity.
Related
I tried several things, such as:
Intent i = new Intent(this, myActivity.class);
startActivity(i)
Where it tells me it cannot resolve symbol 'myActivity'
I tried Context.startActivity and extended the class by Context, at that point it just wants me to implement every single method of Context into my class.
How can I just simply make a new activity visible to the user after an if condition runs into true?
Intent i = new Intent(CurrentActivity.this, myActivity.class);
startActivity(i)
Here myActivity is the name of your second activity (Which you want to open from this intent).
CurrentActivity is the name of your current activity
First you need to understand how to define a class name(Should always start from a capital letter). Then replace the 'myActivity.class' with your new class name.
Make sure that your activity is defined in the AndroidManifest.xml
<activity android:name=".YourActivityName" />
if this doesn't fix the problem then try:
Intent myIntent = new Intent(view.getContext(), MyClass.class);
if view is not found, then implement your class with
public class YourClassName extends AppCompatActivity implements View.OnClickListener{#Override
public void onClick(View v) {
}}
or if you are in a fragment then:
Intent myIntent = new Intent(MyFragment.this.getActivity(), MyClass.class);
#Override
public void onClick(View v) {
int i = v.getId();
if (i==R.id.cars){
Intent open_adapter_class = new Intent(this,ItemsAdapter.class);
startActivity(open_adapter_class );
}
}
//this is the error I get
android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.dikolobe.salesagent/com.dikolobe.salesagent.ItemsAdapter}; have you declared this activity in your
AndroidManifest.xml?
I know that am getting this error because adapter is not an activity so I just want to get help on how to
open this adapter class if there is a way to do so
From Android documentation:
An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested BroadcastReceiver
components, and Context.startService(Intent)
So, ItemsAdapter has to be an activity, (eg ...extends AppCompatActivity) so you change from one activity to another.
But what you can do is pass an object from that class to another activity
Intent open_adapter_class = new Intent(this,ItemsAdapter.class);
I was wondering if anyone could tell me how I could check a checkBox in an activity from another activity.
I'm making a homework app and I want to put a check next to the questions that have been completed.
So the first activity has a list of questions and next to them are unchecked boxes. When you click a question, the app takes you to the second activity. In the second activity, I want to check the box of the question that was completed.
You should use SharedPreferences for that. call this in the first activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.putBoolean("KEY", <your value>).apply();
and something similar in another activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean isTicked = prefs.getBoolean("KEY", <default boolean value>);
where KEY can be anything, for example, your question's number
You should use intent and bundle logic for passing data between activities.(in most cases)
In the first activity, whenever you are creating the second activity, you pass your data.
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("checked",checkBox.isChecked());
startActivity(intent);
In the second activity you receive your data by using :
Intent receivedIntent = getIntent();
boolean finalChecked = receivedIntent.getExtras().getBoolean("checked");
//now you can apply your logic with finalChecked variable
You can save checked check question to a bundle and pass it as extra to your second activity through intent and read it from that intent inside your second activity
In your first activity do something like this
public class FirstActivity extends Activity {
public static final String EXTRA_CHECKED_QUESTION = "Checked Question";
// other codes here
private void startSecondActivity() {
String checkedQuestion = getCheckedQuestion(); // a method to get checked question
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(FirstActivity.EXTRA_CHECKED_QUESTION, checkedQuestion);
startActivity(intent);
}
}
then in your second activity
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstance) {
// other codes
String checkedQuestion = getIntent().getStringExtra(FirstActivity.EXTRA_CHECKED_QUESTION);
// do whatever you want with checkQuestion
}
}
Please note that the string which you pass as first parameter to putExtra() method in FirstActivity, should be same as the string you are using with getStringExtra() to retrieve your extra in SecondActivity, otherwise you can not retrieve your passed extra in SecondActivity.
Also, I did not write these codes inside idea, so there might be syntax error
Read more about intent here
I have 2 variables in lets say class "first" and I want these values in class "second" to do some task.
What could be the simplest way.
As I am working on android development the 2 variables values are edittext values entered by the user
Either you can put them in second class's constructor or make them public static so that using class name you can access them..
Using Static it will be shared by all threads. Better to make private and provide the getter and setter method, hence you can access the value by getter method.
In the first class, you can do something like this,
name_text = (EditText) findViewById(R.id.nameText);
String name = name_text.getText().toString();
Intent intent = new Intent(first.this, Second.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",name);
startActivity(intent);
and in the Second class, you can do something like this,
Intent i = this.getIntent();
String name = i.getStringExtra("name");
If you mean first and Second are Activities then you can user putExtra concept.
FirstActivity.java
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("var1", editText1.getText().toString());
intent.putExtra("var2", editText2.getText().toString());
startActivity(intent);
SecondActivity.java
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String txtOne = bundle.getString("var1");
String txtTwo = bundle.getString("var2");
}
As you are using Intent to navigate from Activity1 to Activity2, make use of Intent.putExtra("key", "Value")
i am curious how i go about using the
putExtra("something", something);
in a intent to a new activity inside my application,
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("something", something)
startActivity(i);
I need to know how i would the sending and receiving end would look like, For Loading a specific layout file depending on what was sent via the putExtra in the intent.
So if i send the i.putExtra("a", a) on the receiving end, it would create a content view with R.layout.a_layout.xml
but if i sent i.putExtra("b", b) it would load R.layout.b_layout.xml
Any ideas?
One nifty way of doing this would be to say:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("layout", R.layout.a_layout.xml);
to send the correct layout to your new class. Then in the new class, you can use:
int layout = getIntent().getIntExtra("layout", R.layout.default_layout);
setContentView(layout);
Send the int representing your layout:
i.putExtra("layout_id", R.layout.my_layout1);
and load it
setContentView(getIntent().getIntExtra("layout_id", R.layout.default_layout);
A value such as R.layout.a_layout is just an int. So, just use putExtra("Key", R.layout.a_layout);.
Then, on your receiving activity, just pass that same int to setContentView(), like:
public void onCreate(Bundle savedInstanceState)
{
int layout = getIntent().getIntExtra("Key", -1);
if (layout != -1)
setContentView(layout);
}