I recently started coding in Eclipse and I haven't done much yet so this is more or less my first app. I'm trying to make my school scheudele, it's simple: first activity shows 5 buttons, each button leading to a new activity (monday - friday).
How would I make so that when I click a certain button a new activity (let's say monday) would pop up?
I've seen hundreds of these questions already asked and answered on here but I just don't get it. It's useless to copy & paste code from here if I still don't get what's going on. I know I have to create a new intent and buttonlistener but I just don't get it what for and what to do then.
Could someone explain it to me as detailed as you can how exactly switching between activities using buttons work and how to actually do it?
I have:
MainActivity.java
Monday.java
5 buttons (button1-5)
So how would I code button1 to switch from MainActivity.java to Monday.java?
Start by adding android:onClick="onClick" to each of your buttons' XML elements. This will make your buttons execute the onClick method whenever an onClick event is triggered on them.
Then in your MainActivity class add the following method:
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// Monday
Intent intent = new Intent(MainActivity.this, Monday.class);
startActivity(intent);
break;
case R.id.button2:
// Tuesday
Intent intent = new Intent(MainActivity.this, Tuesday.class);
startActivity(intent);
break;
// the rest of the buttons go here
default: Log.e("YourTAG", "Default in onClick hit!");
break;
}
}
So every time there is an onClick event on any of your five buttons, the onClick method above will execute with the argument representing the View you just clicked on.
Details regarging intents and how they work here
And as #Edward noted, don't forget to add your new activities in your AndroidManifest.xml file under the application element, such as:
<activity android:name=".Monday" android:label="#string/app_name"></activity>
what you should do is to create an Intent that will fire your Activity you do that by this code:
startActivity(new Intent(YourCurrentActivity.this, Monday.class));
You will have to fire a different Intent on each button that will create the appropriate Activity.
Of course don't forget to declare your Activitys in your manifest file.
OK. Lets say you have following button in your layout file:-
<Button
android:id="#+id/my_btn"
android:layout_width="55dp"
android:layout_height="22dp"
android:onClick="goToMonday" // function name which will be in MainActivity.java
/>
Now in your MainActivity.java:-
public void goToMonday(View v){
Intent monday_intent = new Intent(MainActivity.this, Monday.class);
MainActivity.this.startActivity(monday_intent);
}
This is the way to change activities.
Related
I'm creating an application to which I've added a Botton Navigation View but I don't know if there is any way that it appears in all the Activity or if on the contrary I would have to specify it in each of them to appear, as well as adding all its functionality on each screen, since this last option I see a little cumbersome.
The other question is for the button Home of this bar, I have added a button that is home to return to the main Activity each time I pulse, in the way that I have done has been with an Intent that goes to that screen itself, but I do not know if there is another method:
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.btnHome:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
break;
Check out how to use Fragment in Android. You don't have to open a new activity each time the button in your BottomNavigationView is clicked, instead, you use fragments, so only the selected part in activity is changed. Check out the following tutorial to learn more about Fragments. See the "dynamic creation" of fragments.
How to use Fragments in Android.
In your case, there'll be code of FragmentTransaction where you're launching a new Activity using Intent. You'll be replacing only a certain portion of the screen(between your Toolbar and `BottomNavigationView).
This question already has answers here:
Android: Go back to previous activity
(24 answers)
Closed 3 years ago.
I have two activities, How is it possible to go back to a previous activity.
What code do I need to go back to previous activity
Assuming you started the new activity with
startActivity(new Intent(this, SecondActivity.class));
puts the SecondActivity in front and the FirstActivity in the backstack. To go back to the FirstActivity, put this in your second activity.
finish();
If you start the activity with result like below :
From 1st Activity :
startActivityForResult(new Intent(this, SecondActivity.class),requestCode);
You can finish the activity with required intents :
From 2nd Activity :
// Optional if you want to pass values from second activity to first
Intent data = new Intent();
data.putExtra("key","any_value");
setResult(RESULT_OK,data);
// Just finish
finish();
Refer the below link for more information like onActivityResult callback and more
https://developer.android.com/training/basics/intents/result
1st Method
If you are using Intent then make sure you don't call finish(); method after using the following code,
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
By this code, the Back Button you created will do its work and go to previous Activity.
And also don't override the onBackPressed() method in Activity.
2nd Method
There is another way that you can achieve this by setting Home Button to go to specific Activity, For that, you have to create Back Button in Action Bar in onCreate() method.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
And in the AndroidManifest.xml file, you have to add the following,
<activity
android:name="com.example.yourapplication.SecondActivity"
android:label="#string/title_second_activity"
android:parentActivityName="com.example.yourapplication.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourapplication.MainActivity" />
</activity>
using intent
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
Or you can use onBackPressed() if the activity added to backstack
onBackPressed();
As we know the main activity in Android, Eclipse is called MainActivity.java
Actually I have two activites, the second one is: Page2Activity.java
And I have a Page2.xml too for the layout.
I would like to know how can I switch to Page2Activity.java when pushing a button? Because only Page2.xml shows up, and when I click on a button to play a sound nothing happens on the second page.
MainActivity.java
...
bpage2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.page2);
}
});
...
When I push this button, page2.xml shows up, but it contains sound from Page2Activity.java and when I hit a button the sounds won't play. Could you please tell me how can I load Page2Activity.java with layout page 2?
Regards,
Henrik
You have to fire an event on click action for the button and start page2 Activity on it's listener.
bpage2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, Page2Activity.class);
startActivty(i);
});
Why don't you start the other activity Page2Activity when you click on yout bpage2-Button?
Put this in your listener instead of setContentView(R.layout.page2); :
Intent intent = new Intent(this, Page2Activity.class);
startActivity(intent);
Calling setContentView() sets the layout of your activity - that's why you see the other layout after clicking the button. So what you do is just switching the layout but the logic (e.g. attached listeners) is missing and needs to be implemented. That's why the sound doesn't play. The layout coexists with the logic but it's independant from the logic. Including a xml-layout doesn't mean you include the logic too.
Check out the Fragments http://developer.android.com/guide/components/fragments.html.
In addition take a look on this introduction on how to start an activity from another one: http://developer.android.com/training/basics/firstapp/starting-activity.html .
Maybe you should even start here http://developer.android.com/guide/components/activities.html :)
Good luck.
In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
I have following 2 activities(plus many more not important for this question):
<activity android:name=".activities.HomeActivity" android:excludeFromRecents="true" />
<activity android:name=".activities.AdHocActivity" android:noHistory="true"/>
HomeActivity is a first one and only one wich I keep history for. User can go differnt places from Home and click Back to come back to main HomeActivity.
I also have service running on alarm and checking for some specific things. When specific criteria met I'm displaying my AdHocActivity
Intent i = new Intent(context, AdHocActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(BlockingActivity.INTENT_BLOCKED_PACKAGE_NAME, packageName);
context.startActivity(i);
FLAG_ACTIVITY_NEW_TASK was necessary to show Activity from BroadcastReceiver
Now, this AdHoc activity displays some message to user and has a button to take user back to HomeActivity
private void sendToMainApplication()
{
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
finish();
}
All this work, but I get second instance of HomeActivity in a stack. So now when user taps "Back" - my Home activity flashes and comes back (previous copy).
I want only one copy to stay on top. I want it to be like anchor one. If it doesn't exist - I want new one to be opened.
The trick to prevent HomeActivity from being doubled was to call it like this:
private void sendToMainApplication()
{
Intent i = new Intent(this, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}