I want to keep my app as loosely coupled as possible, and most of is done with IoC
however, at some point, i need to launch different activities,and the class implementing this activity, could be any,meaning i don't want to define a specific class that starts the activity, but one needs to be set in the intent.
where is the best place to write the code to launch my activities ? is it in the same activity that starts the other activity? or have some outside logic about it?
I have an activity A
from which i need to start activity B
where do i put the logic of
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, requestingB);
It sounds like you're trying to have an activity that can be launched by some other application and you don't want the activity to necessarily know about what is launching it.
Try using an intent-filter within your activity. Then, when something needs to launch it, all it has to do is fire off an intent with the action defined in the intent-filter.
As always, Vogella has a good tutorial here: http://www.vogella.com/tutorials/AndroidIntent/article.html
As the OP mentioned in comments that he wants to start another activity on the click of a button, below is the sample code:
Button myBut = (Button) findViewById(R.id.but1);
myBut.setOnClickListener(new onClickListener()
{
#override
public void onClick(View view)
{
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
}
});
Hope this helps
Related
Suppose I have two activities A and B activity A which contains a button I want to start Activity B when I press Button without intent.
According the Oficial 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 startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
So you have to use it to open activities with no exceptions or workarounds, if you do that, you are ignoring the entire system architecture.
There is no way to start an activity from anotherone without an intent.
If the reason of not using Intent that you don't want the the user to re-enter the previous activity
You can use finish() to finish that activity intent after you done work with
if(currentUser == null){
startActivity(new Intent(MainActivity.this,StartActivity.class));
finish();
}
So user will be unable to back again
If you want to do some code while the activity is finishing
You can use onDestroy() override method, Sometimes it can also be called if the activity is being killed by the android itself so you can add
isFinishing() function
Inside onDestroy() method which checks whether the application is closing by the call finish() returning true or otherwise by anything else returning false then you can easily specify your code for each situation.
#Override
protected void onDestroy() {
super.onDestroy();
if(isFinishing()){
// Activity is being destroyed by the function `finish()`
// What to do...
}else{
// Activity is being destroyed anonymously without `finish()`
// What to do...
}
}
Put your activity inside a Fragment and start the fragment fromo the button.
These are the possible ways to start any Activity
1st
startActivity(new Intent(Activity_A.this, Activity_B.class));
2nd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivity(intent);
3rd
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivityForResult(intent,code);
If I create a new Intent of the same class every time I click a button, is the created activity the same?
Every time I click a button, I want to have a dialog show with a slider inside it and after I change it I want the state to be saved so that the next time I open up the dialog the state of the slider is the same.
My code for the button is this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Slider_Logic.class);
v.getContext().startActivity(intent);
}
});
By "same," I assume you mean the same object instance. The answer is no. In general, when you start a new activity, it creates a new instance of that activity and pushes it onto the stack in front of the existing activity.
I say "in general" because the activity's launch mode can effect this behavior. For example, if you set launchMode to singleTop, it will create a new instance of the activity if one doesn't already exist in the target task. Please see the docs for more information.
I have two activities, A and B. I have a button in A,when pressed starts a new intent to B.
However whenever I press the back button while in B, than click the button again is restarts Activity.I do not want to do that,I want it to resume Activity B.
Actually I am doing some networking in Activity B and I want to save unless the user wants to refresh.
How can I do it? Thanks in advance
Use
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
Hope, It must help you
You need to Overrider the
onBackPressed
method and start there the activity like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, activityA.class);
startActivityForResult(intent, PICK_CONTACT_REQ);
}
This sounds like you need to rethink your architecture. You say:
Actually I am doing some networking in Activity B and I want to save
unless the user wants to refresh.
If this is the case, you probably don't want to do your networking in ActivityB. Maybe you should start a Service from ActivityB to do the networking. The service and the activity can communicate with each other so that the activity can keep the user up-to-date about the state of the networking. If the user presses BACK in ActivityB, it can finish (as usual) and return to ActivityA. In this case, the Service is still managing the networking. When the user again starts ActivityB (from ActivityA), the new instance of ActivityB can communicate with the service to see if there is any networking going on, and if so it can get the current status of that networking or start it or stop it or whatever.
I guess I'm too late but I had a similar problem.
I had two activities A,B and a next button in A.
Whenever I tried to do: A->press next button ->B->press back button->A->press next button->B, B screen got destroyed when I pressed the back button. So when I came back to B for the second time it was a newly created B (all the information I had put in was gone).
So it was like A->B->A->new B when I just wanted to go back to the original B! :(
So what I did was, in activity B, I overrode the onBackPressed() function so it doesn't destroy the activity. I also set the flag so that if there is a A activity already running, it would just pull it up to the front instead of creating a new A activity.
#Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
Then, for the onclicklistener function for the next button in activity A, I set the flags similarly.
public void onClickNextbutton(View v){
Intent intent = new Intent(getApplicationContext(), ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
How to connect GUI screens in Android? I have created several screens and I want to connect these using buttons.
It depends what else you want to do by clicking a button. If you only want to click a button and go to next activity or you want to send some data to next activity. Or maybe you want to dial a number or view contacts etc.
Here's a nice tutorial about using startActivity() and startActivityForResult() methods - android startActivity and startActivityForResult
Also basic Intents Tutorial will be useful.
just take a event handler .. in that use INTENT
Here s the syntax , startActivity(Intent(activity1.this, activity2.class);
ex:
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(activity.this, activity2.class);
startActivity(i);
}
});
Here !st activity1 i..e.. ur current class(1st page) name
Intent is the best mechanism to connect your component(activties, services) run time. here you can find about intent class.
is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity