I am trying to reload my activity and pass a bundle, but I'm getting an empty (null) bundle.
Reload activity:
Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
MyActivity.this.finish();
startActivity(intent);
onCreate activity and I should get the bundle:
#Override
public void onCreate(Bundle savedInstance)
{
if (savedInstance != null)
{
}
else
{
Log.i("d", "IS NULL !");
}
}
I'm getting null.
In OnCreate() you should do like this :
if(getIntent().getExtras() != null) {
Bundle extras = getIntent().getExtras();
Log.i("Value", extras.getString("key"));
}
Instead of this
if (savedInstance != null){
}
First start the activity and then call finish() as follows:
Intent intent = new Intent(MyActivity.this, MyActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", 1);
intent.putExtras(bundle);
startActivity(intent);
MyActivity.this.finish();
Then receive the bundle extras like this:
Bundle bundle = getIntent().getExtras();
Finally, you can put conditions to check if it's correct like:
if(bundle != null)
{
}
else
{
Log.i("d", "IS NULL !");
}
Related
Hi there I want to call an Activity an decide which is the next activity to start after the Dialog finished like:
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra(MyActivityA.EXTRA_PARENT, MyActivityB.class);
I am getting the extra like this in the Oncreate of MyActivityA:
Type _parent = null; // this is a class variable
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext())
{
String key = it.next();
if(key.equals(EXTRA_PARENT))
_parent = (Type)bundle.get(EXTRA_PARENT);
}
}
in the finishDialog method I do this :
public void finishDialog(View v)
{
try
{
Intent intent = null;
if(_parent != null && _parent instanceof Activity)
{
intent = new Intent(this, _parent.getClass());
}
else
{
intent = new Intent(this, DefaultActivity.class);
}
if(intent != null)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
finish();
}
2 Questions:
Why does _parent instaneof Activity not work. I think MyActivity is a Type which inherits of Activity
Trying to start the activity does not work. But if i put in
new Intent(this,MyActivityB.class);
it works! What am I doing wrong. Is there any other way to do this
You can simply pass integer value and map it to corresponding activity.
Intent dialog_intent = new Intent(_parent, MyActivityA.class);
dialog_intent.putExtra("EXTRA_PARENT", 1);
Now in your activity get this EXTRA_PARENT and call activity
Bundle extras = getIntent().getExtras();
int extraParent = extras.getInt("EXTRA_PARENT");
if(extraParent == 1)
{
Intent dialog_intent = new Intent(_parent, MyActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
}
I am trying to save some data in "MainActivity" when switching to another activity, and restoring that data as I switch back to it.
In "MainActivity": (restoring data)
protected void onCreate(Bundle savedInstanceState) {
// do usual stuff
restoreData();
}
In "MainActivity": (switching to "StatusActivity"):
Bundle data = saveData();
Log.d(TAG, "Sending data to status activity intent: " +data);
Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
intent.putExtras(data);
startActivity(intent);
In "StatusActivity":
Bundle data = getIntent().getExtras();
Log.d(TAG, "Sending data to main activity intent: " +data);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtras(data);
startActivity(intent);
My saveData() function:
private Bundle saveData() {
Log.d(TAG, "Started saving state");
Bundle data = new Bundle();
// store stuff in the bundle
return data;
}
My restoreData() function:
private void restoreData() {
Log.d(TAG, "Started restoring state");
Bundle data = getIntent().getExtras();
// restore stuff in the bundle
}
LogCat:
My bundle is fine when sending to StatusActivity:
Sending data to status activity intent:
Bundle[{obj0=Bundle[{timeSinceLastPooped=3224, hunger=5,
id=2130837505, timeSinceLastHungerUpdate=3224,
timeSinceLastFed=0, timeSinceLastHappinessUpdate=3224,
timeSinceLastEvolution=3224, posY=0.0, posX=0.0,
isDead=false, happiness=5, evolutionStage=0, type=pet}],
time=7.794168E7}]
But then sending back to MainActivity:
Sending data to main activity intent: Bundle[mParcelledData.dataSize=648]
What do I do with mParcelledData to get the original bundle back? Thanks!
Answer:
In "MainActivity": (restoring data)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == 1) {
if(resultCode == RESULT_OK) {
restoreData(intent);
}
}
}
In "MainActivity": (switching to "StatusActivity"):
Bundle data = saveData();
Log.d(TAG, "Sending data to status activity intent: " +data);
Intent intent = new Intent(getApplicationContext(), StatusActivity.class);
intent.putExtras(data);
startActivityForResult(intent, 1);
In "StatusActivity":
Bundle data = getIntent().getExtras();
Log.d(TAG, "Sending data to main activity intent: " +data);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtras(data);
setResult(RESULT_OK, getIntent());
finish();
You can use onRestoreInstanceState() witch is called after onStart(), whereas onCreate() is called before onStart().
Use the put methods to store values in onSaveInstanceState():
protected void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putInt("value", value);
}
And restore the value in onCreate():
public void onCreate(Bundle bundle) {
if (bundle!= null){
value = bundle.getInt("value");
}
}
Looking all around, I can't seem to find any way for an Activity to determine with what resultCode it was started. Example:
class A extends Activity {
void yadda() {
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, 8675309);
}
}
class B extends Activity {
void yadda() {
int code = getTheResultCode(); // I want to get 8675309
}
}
Do I have to put it into the Intent?
intent.putExtra("resultCode", 8675309);
In the Activity that knows the resultCode:
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("resultCode",resultCode);
startActivity(i);
In the next Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String resultCode= extras.getString("resultCode");
}
Hi am newbie to java and android.
Assume function demo() from the screenone is going to display some values on Textview which is on the same screen(screenone).
But i need to display that resultant value to the next screen ie.(screen two)
public void demo(){
{
.....
.....
}
So i have included these line to
screenoneActivity.java
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
demo();
ScreentwoActivity.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
txtName.setText(name);
I did these things so far.I don't know how to do transfer data from demo() function to the next screen.
Can anyone give me clues or ideas to achieve this.
Thanks a lot!..
You need to send some value into the putExtra methods parameters to be able to get something out of it.
In your first activity(A):
Intent i = new Intent(A.this, B.class);
i.putExtra("someName", variableThatYouNeedToPass);
startActivity(i);
In your second activity(B):
Bundle extras = getIntent().getExtras();
int fetchedVariable = extras.getInt("someName");
Write the below code in demo() function:
Intent nextScreen = new Intent(getApplicationContext(), SecondtwoActivity.class);
nextScreen.putExtra("","");
startActivity(nextScreen);
In nextScreen.putExtra("",""); provide some key and value like:
nextScreen.putExtra("name","ABC");
Now in SecondActivity, write:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
TextView txtName = (TextView) findViewById(R.id.textView1);
Intent i = getIntent();
Bundle bundle = i.getExtras();
txtName.setText(bundle.getString("name"));
In ScreenoneActivity
Intent act2=new Intent(this,Activity2.class);
act2.putExtra("A",a);
startActivity(act2);
In ScreentwoActivity class
Intent i = getIntent();
Bundle extras = getIntent().getExtras();
int a = extras.getInt("A");
txtName.setText(a);
in onCreate :
Bundle extras = getIntent().getExtras();
String value;
if (extras != null)
{
value= extras.getString("key");
}
https://stackoverflow.com/questions/10752501/how-can-we-go-to-next-page-in-android/10752516#10752516
google it is very basic .....
android using intent....
Vogella Ariticle
in activity 1-
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
startActivity(i);
In activity 2 - in onCreate finction
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
}
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
// Do something with the data
}
I know there are questions like:
android-intent-bundle-always-null and intent-bundle-returns-null-every-time but there is no correct answer.
In my Activity 1:
public void goToMapView(Info info) {
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
}
In Info:
public void write(Intent intent) {
Bundle b = new Bundle();
b.putInt(AppConstants.ID_KEY, id);
... //many other attributes
intent.putExtra(AppConstants.BUNDLE_NAME, b);
}
public static Info read(Bundle bundle) {
Info info = new Info();
info.setId(bundle.getInt(AppConstants.ID_KEY));
... //many other attributes
return info;
}
In MapViewActivity (Activity 2):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);
Bundle extras = getIntent().getBundleExtra(AppConstants.BUNDLE_NAME);
info = Info.read(extras);
...
}
Problem is that extras bundle is always null. I have debugged it and Intent (intent = getIntent()) has all fields set to null except one indicating what class this is (MapViewActivity).
I've also tried putting the bundle via intent.putExtras(b) with the same effect.
The intent.putExtra("asdf", true) is for debugging reasons only - I cannot get this data too (because getIntent() has almost all fields set to null).
EDIT
The answers below are correct and working. This was my fault. I didn't correctly passed my bundle to new intent.
I am not sure what "Info" is for, but I suggest making the most basic passing of data from one activity to another first before involving other data objects.
Activity1
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("asdf", true);
info.write(intent);
startActivity(intent);
Activity2
Bundle bundle = getIntent.getExtras();
if (bundle!=null) {
if(bundle.containsKey("asdf") {
boolean asdf = bundle.getBooleanExtra("asdf");
Log.i("Activity2 Log", "asdf:"+String.valueOf(asdf));
}
} else {
Log.i("Activity2 Log", "asdf is null");
}
Activity 1
Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
Bundle b = new Bundle();
b.putBoolean("asdf", true);
b.putInt(AppConstants.ID_KEY, id);
intent.putExtras(b);
startActivity(intent);
Activity 2
Bundle extras = getIntent().getExtras();
boolean bool = extras.getBoolean("asdf");
int m_int = extras.getInt(AppConstants.ID_KEY,-1);