How to decide which Activity will start after the next in android - android

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);
}

Related

Why I can NOT receive extra parameters from Intent in onStartCommand? [duplicate]

Is there any way to check if an extra has been passed when starting an Activity?
I would like to do something like (on the onCreate() in the Activity):
Bundle extras = getIntent().getExtras();
String extraStr = extras.getString("extra");
if (extraStr == null) {
extraStr = "extra not set";
}
But this is throwing a java.lang.NullPointerException.
Thank you.
Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.
Example:
Intent intent = getIntent();
if (intent.hasExtra("bookUrl")) {
bookUrl = b.getString("bookUrl");
} else {
// Do something else
}
Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.
Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.
here is how IN MY CASE I solved it:
Intent intent = getIntent();
if(intent.hasExtra("nomeUsuario")){
bd = getIntent().getExtras();
if(!bd.getString("nomeUsuario").equals(null)){
nomeUsuario = bd.getString("nomeUsuario");
}
}
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
// intent is not null and your key is not null
}
I think you need to check when extras != null
Bundle extras = getIntent().getExtras();
if (extras != null) {
String extraStr = extras.getString("extra");
}else {
extraStr = "extra not set";
}
I would use this solution in your case.
String extraStr;
try {
extraStr = getIntent().getExtras().getString("extra");
} catch (NullPointerException e ) {
extraStr = "something_else";
}
Working Code
If you want to check first Intent have no extra
Intent intent = getIntent();
if (intent.getExtras() == null){
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}else {
if (intent.hasExtra("type")) {
String type = getIntent().getStringExtra("type");
switch (type){
case "showRateUsDialog":
Intent i = new Intent(Splash.this, Main.class);
i.putExtra("type", "showRateUsDialog");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
case "refer":
Intent i2 = new Intent(Splash.this, Refer.class);
i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i2);
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
break;
default:
startActivity(new Intent(Splash.this, Main.class));
overridePendingTransition(R.anim.enter, R.anim.exit);
finish();
}
}
}
}

I am trying to make a login activity which proceed to another activity showing name of the user.

I am trying to make a login activity which proceeds to another activity by clicking. but its giving me error. here is the code....
public void onButtonClick(View v) {
if(v.getid() == R.id.Blogin)
{
EditText a=(EditText) findViewById(R.id.TFUserName);
String str= a.getText().toString();
Intent i = new Intent(this, SignIn.class);
i.putExtra("Username",str);
startActivity(i);
}
}
its giving me error in view and getid.. any one can help me in solving this error?
Try by this code .
First Activity:-
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
String keyIdentifer = null;
i.putExtra("STRING_I_NEED", strName);
Second Activity :-
String newString;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String)savedInstanceState.getSerializable("STRING_I_NEED");
}
complete code ..
or
you can use shared Preference to save values of user Login.

Reload same activity but pass bundle

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 !");
}

I cant send Extra for singleTask activity Android

I use singleTask activity in my application.
Order activities A->B->C->B
I put extra on A and then get it on B, then I go to C and try putExtra for B, but on B I don't see it.
If I use default android:launchMode, it work ok.
you have to carry extra between intents.
A -step1-> B -step2-> C -step3-> B
step1
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String extraString;
Bundle extras = getIntent().getExtras();
if (extras == null) {
extraString = null;
System.out.println("null extra");
} else {
extraString = extras.getString("extra");
System.out.println("from " + extraString);
}
Intent intent = new Intent(C.this,B.class);
intent.putExtra("extra", extraString);
startActivity(intent);
};
step2
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String extraString,extraString2;
Bundle extras = getIntent().getExtras();
if (extras == null) {
extraString = null;
System.out.println("null extra");
} else {
try
{
extraString = extras.getString("extra");
System.out.println("from " + extraString);
{
catch{}
}
// CATCH EXTRA STRING2
if (extras == null) {
extraString2 = null;
System.out.println("null extra");
} else {
try
{
extraString2 = extras.getString("extra2");
System.out.println("from " + extraString2);
{
catch{}
}
Intent intent = new Intent(C.this,B.class);
intent.putExtra("extra", extraString);
startActivity(intent);
};
step3
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String extraString
Bundle extras = getIntent().getExtras();
if (extras == null) {
extraString = null;
System.out.println("null extra");
} else {
extraString = extras.getString("extra");
System.out.println("from " + extraString);
}
Intent intent = new Intent(C.this,B.class);
intent.putExtra("extra2", extraString);
startActivity(intent);
};

Bundle is null after setting it in Intent

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);

Categories

Resources