How to jump from one activity to another particular activity in Android? - android

I have 3 activities in Android like activity_A,activity_B and activity_C. Calling sequences like this activity_A >>>>>>> activity_B >>>>>> activity_C. Currently I am in activity_C and now I want to jump directly to activity_A without entering into activity_B and I also have to pass some data.
HOW ???? Please give some piece of code for it!!!
Thanks in Advance !!!
Arthur

If you want to add a new copy of Activity_A on top of the stack, just call it the same way as you did before. Otherwise, choose a launch mode depending on what you want the first copy of Activity_A to do. See this question (method #2) for an explanation of passing data between activities.

You can use similar code as you used to get from A>>>B and B>>>C.
Or you can use finish() to exit your Activity. In your case, you'd set a flag in C to tell B to finish then call finish() in C. In B, look for that flag in the onResume() method and finish() B if it exists.

From your ActivityC code:
Intent intent = new Intent(this, ActivityA.class);
intent.putExtra("a key string", data);
startActivity(intent);
In ActivityA you can recover the data using:
Bundle extras = getIntent().getExtras();
if (extras != null) {
// here use the getters in extras to retrieve the appropiate data type
// for example if your data is a String the you would call
// extras.getString("a key string");
}

Specific code will depend on the type of data. But you can do something like this:
Intent i = new Intent(getApplicationContext(), ActivityA.class);
i.putExtra("myData", someData);
startActivity(i);

Related

How to return to second activity and show the change value I made before

I have two activities, A & B. A has a button to go to B. B sets some parameters using Seekbars.
When I go back to A there is no problem. But, I when again go to B, the Seekbars do not show the changed value.
I tried looking for solutions and I came to know about "Intent" class but the concept is not clear to me.
What is a simple clean solution to see the changed values when going to the Activity B again from Activity A?
When we want to move between activities, we use Intent. For e.g. If I have two activities ActivityA and ActivityB, I will do something like this:
Intent intent = new Intent (Activity.this, Activity.class) ;
startActivity (intent) ;
But I want to pass some values from one activity to another, Intents are also useful for that. Lets say I want to pass some string value:
In ActivityA (before starting the activity):
Intent intent = new Intent (Activity.this, Activity.class) ;
intent.putExtra("key", "value");
startActivity (intent) ;
And in ActivityB (inside onCreate) :
Intent intent = getIntent() ;
String test = intent.getString("key");
And here you have your value. You can also pass objects if you want by implementing serializable/parcelable class. Read about it:
How to pass value using Intent between Activity in android
https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
You should use intent.putExtra() and intent.getExtra().
You can see the example here; https://stackoverflow.com/a/14017737/8883361

How can I not return to a previous activity after using startActivity?

Assuming I have three activities with Android Studio:
Act_A
Act_B
Act_C
The Act_A calls Act_B, and Act_B calls Act_C:
Intent intent = new Intent(Act_A.class,Act_B.class);
startActivity(intent);
...
Intent intent = new Intent(Act_B.class,Act_C.class);
startActivity(intent);
In Act_C a process is executed in which it should automatically be returned to Act_B, for which something similar is done:
Intent intent = new Intent(Act_C.class,Act_B.class);
startActivity(intent);
At this point, if the user presses the return button, it should be returned to Act_A, but it happens to be returned to Act_C, if you press once again the return button returns to Act_B.
My questions are:
Is there a way to "delete" the previous state so that it doesn't return to the previous activity or is there a way to modify it to return to the activity that I want?
The problem is that I have to return a value from Act_C to Act_B and I can not use finish(), something similar to the following:
In Act_C:
Intent intent = Act_B.newIntent(getActivity(),5);// return 5
startActivity(intent);
Thanks
You can call the finish() method of an Activity. It will get you back to Activity_B while you are in Activity_C
You can customize the back stack of your activity if you really need to, but in this case it seems that what you want is just to go back to B from C, instead of starting B from C.
In other words, in order to accomplish "In Act_C a process is executed in which it should automatically be returned to Act_B", do the following instead of startActivity():
super.onBackPressed();
This will return the app to Act_B. Then if the user presses the back button, they will return to Act_A as you specified.
Basically you can either call finish() on your activity, use the noHistory flag for the activity in your AndroidManifest.xml, or add the intent flag Intent.FLAG_ACTIVITY_NO_HISTORY to your intent.
This post should help you out: Removing an activity from the history stack
In Act_C a process is executed in which it should automatically be returned to Act_B
If you just want to return a value from a specific activity don't use startActivity(Intent), use startActivityForResult(Intent, int) and deal with the return values on the callback (OnActivityResult). Then overwrite the onFinish() method so that you can setResult() upon exit.
I can give you more context on this if you need.

Android activity getting old values in bundle

In my android activity B, I read values bundled in the intent like this
Bundle bundle = getIntent().getExtras();
Boolean mine = bundle.getString("mine").equals("1");
int pagenum = bundle.getInt("page");
When I start B from another activity A, I give in mine=0,pagenum=0.
And I can read that fine in B.
But then in B, I want to reload the activity, by finishing itself and opening another B. I also need to pass in the new data like this:
private void refresh(Boolean mine, int newpage) {
finish();
Intent myIntent = new Intent(this, AllThreadsScreen.class);
myIntent.putExtra("mine", mine ? "1" : "0");
myIntent.putExtra("page", Integer.toString(newpage, 10));
startActivity(myIntent);
}
When I call this, I make sure that newpage has a value of 1. However the problem is that, after starting the activity, when I read the page value from the bundle, it becomes 0 again...
Does anyone know whats wrong?
Thanks.
You are not "restarting" the Activity. If you call finish() and the lines after still work, then it ins't really finished, is it?
What is happening is the Activity may begin the process of finishing, but then receives a new Intent. Android doesn't actually destroy it.
Also, you don't need to finish the Activity anyway. just use onNewIntent (a little known, little used Activity method).
How to capture the new Intent in onNewIntent()?

How to send data from one activity to another by using getIntent() instead of usinf new Intent();

usually we send data from one activity to another by using:-
Intent i=new Intent("<action name>");
i.putExtras("name",data);
startActivity(i);
My question is can we send data from one activity to another by using:-
Intent i=this.getIntent();
i.putExtras("name",data);
setResult(Activity.RESULT_OK,i);
finish();
If yes please explain the concept.
Also,these two classes are in the different projects in Eclipse.My another question is, is it possible to send data through intent to another activity situated in another project??
So you're starting from Activity A and going to Activity B using startActivityForResult()
Now we're in Activity B and want to go back to Activity A:
Intent i = new Intent();
if(getIntent().getExtras() != null) i.putExtras(getIntent().getExtras());
setResult(Activity.RESULT_OK, i);
finish();
Doing something like that will allow you to pass back the extras from the calling Intent (if they exist), which would then be reachable from Activity A's onActivityResult() method.
Yes. You can do so, when you call another intent for a result. ie startActivityForResult(Intent,Request_Code);

Launching activity through intents

I am implementing a simple app. I need to start an activity based on the state of the Activity. Lets take i am using a button to start the activity.
1. If the activity is not started, I need to start XYZ activity.
2. If the XYZ activity is on focus, then i need to close the activity on the button press.
3. If the XYZ activity is not in focus (like onPause) state then, I need to change the button state.
Can you please help me in the flags that i need to use for starting the intent.
Is it possible to get the state of activity before I start that activity?
Try this
Intent intent = new Intent(currentActivity.this, callingActivity.class);
startActivity(intent);
You can use intent like this to call an activity
The button press will need to be captured by each activity separately, so just code the different responses into each different activity.
First off create a MAIN.java activity that is going to house your other activities. Like others have said you're going to have to code the button captures yourself because that should be common sense if you're trying to deal with intents. When you get that together though, you can start a new activity through intent like so:
// allocate new intent, initialized to the activity you wish to launch
Intent i = new Intent(this, ActivityToBeLaunched.class);
// put information into intent
i.putExtras("KeyName", value); // where "KeyName" is simply a reference string
// and 'value' can be anything from boolean - string.
// launch activity and wait for response
startActivityForResult(i, REQUEST_CODE);
Then within your ActivityToBeLaunched.java class you'll have an oncreate that will pull information from the intent like such:
// get intent
Intent i = this.getIntent();
// get information from intent
booleanVariable = i.getExtras().getBoolean("KeyName");
When you're done with this activity simply use;
// create intent
Intent i = new Intent();
// put information into result to send back to parent
i.putExtras("KeyName", value);
// set the result to be returned
setResult(i, ResultCode);
// finish child, return to parent with results
finish();

Categories

Resources