How Do I Play Sound from another java activity? - java

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.

Related

In android Studio, I want when i click on button , next activity/fragment should come from right side

In android Studio, I want when i click on button , next activity/fragment should come from right side and present activity sholud gone left.I implimented its working on Activity but not on adapters is showing error.
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
startActivity(i);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
overridePendingTransition is working on Activity but not working on Adapters of Recyclerview and Listview, Please tell any other option. I want when i click on recyclerview item next Activity should navigate or come from right side by using overridePendingTransition.
Fragment fragment = Fragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit, R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_right_exit);
Utils.addFragmentToActivity(fragmentTransaction, Fragment, R.id
.content_frame);
This tip features how to change Android’s default animation when switching between Activities.
The code to change the animation between two Activities is very simple: just call the overridePendingTransition() from the current Activity, after starting a new Intent. This method is available from Android version 2.0 (API level 5), and it takes two parameters, that are used to define the enter and exit animations of your current Activity.
Here’s an example:
//Calls a new Activity
startActivity(new Intent(this, NewActivity.class));
//Set the transition -> method available from Android 2.0 and beyond
overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
These two parameters are resource IDs for animations defined with XML files (one for each animation). These files have to be placed inside the app’s res/anim folder. Examples of these files can be found at the Android API demo, inside the anim folder.
for example code visit http://www.christianpeeters.com/android-tutorials/tutorial-activity-slide-animation/#more-483
Change like this code you must be passing activity as context in adapter
holder.questions.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(DoctorsProfile.this,Questions.class);
i.putExtra("DOCTOR_ID",doctor_id);
Activity activity = (Activity) context;
activity.startActivity(i);
activity.overridePendingTransition(R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}
});
Note : Context is the Base Object of Activity
Update :
I have checked the accepted answer but hope you understand that it will be called everytime when your activity get launched and thats not supposed to be best practice. I am suggesting better approach if you want to follow the accepted answer .
Alternative :
Pass one parameter in bundle to new activity to make sure that transition coming from that specific adapter so double transation should not happen when you are coming rom any other activity also.
There is a easy way to do this. just put overridePendingTransition on your OnCreate method of next Activity/Fragment.So that when next Activity will come it will come according to your choice.Need not add overridePendingTransition on adapters.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_question);
overridePendingTransition( R.anim.slide_in_right_up, R.anim.slide_out_right_up);
}

Android Back Button deleting activity for good

I am making a game, and when a level is complete I have a pop-up activity which tells you your score.
When I press the back button I want this instance of the pop-up activity to be deleted for good.
Currently I get the behaviour;
Finish level, pop-up appears
I press back button to get rid of it
Start a new level, finish it and new pop-up appears
If I press the back button twice, the new pop-up disappears and the old one re-appears.
I want to delete the old pop-up as soon as it is hidden.
I am using the following code in my pop-up activity;
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
Any help would be much appreciated.
Thanks!
You can use this code. It clears off the PopupActivity form the Top Activity Stack so that it will not appear again.
#Override
public void onBackPressed() {
Intent removeIntent = new Intent(GameActivity.this, PopUpActivity.class);
removeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(removeIntent);
}

How do I open a new activity with a button click?

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.

Prevent back button after logout in android

After logout the user is directed to login screen in android. Now, if user clicks on back button of phone it should stay on login screen itself.
How can I make it possible in android?
I have used following code in my application but it will close my application. It should stay on the login screen only
Intent objsignOut = new Intent(getBaseContext(),Hello.class);
objsignOut.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objsignOut);
Please guide me the correct way.
override the onBackPressed in your login activity, to do nothing..
public void onBackPressed() {
//do nothing
}
It seems to me that there are simpler and cleaner solutions than overriding onBackPressed method, as mentioned here and here.
You can provide flags when launching a new activity (on login or logout) to simply clear the "back-stack" rather than override the behavior for the back-button:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
This is a safer solution that can also be used after you log-in and not just after you log-out.
public void onBackPressed(){
if(appCanClose){
finish();
}
}
These functions can exist in both the system framework (used if not in your code), as well as in your code. If you leave it empty, the app will do nothing when the back button gets pressed.
In this example, when the boolean value appCanClse is true, the back button will quit the app, if false, the back button wil do nothing. I would make sure the user still has someway to quit the app. :p
You can do it by just adding this two line of codes
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
It will prevent going back to previous activity as well as take the app to background when anyone hits back button
The actual solution is
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
add this code in Login Activity. App closes when back button clicked in login page.

Iconsistent results when returning to a previous activity

I have an android app that has various activities.
for example there is a home screen and an update screen.
The home screen is a list activity.
A button on the home screen brings you to the update screen that updates the database from a server. In theory when Returning to teh homescreen after an update, the list should be changed to reflect the update just done.
the code for going to the update screen is as follows:
Button synch = (Button) findViewById(R.id.synchButton);
synch.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
Intent intent = new Intent(HomeScreen.this, SynchScreen.class);
startActivity(intent);
}
});
and the code for returning back to the homescreen after the updates is:
main_menu.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
finish();
}
});
the list is compiled from an async task that runs in onStart, so my understanding is that onStart should run when I return to the homescreen, thus always displaying the most up to date version of the list.
On my Emulator I always get teh updated list, but when I run it on my phone the list is never updated, it just returns to the state of teh screen before I did the update.
any ideas?
thanks
Kevin
Check the Activity lifecycle section of the Android documentation. The code updating the view should probably be moved to onResume, since the Activity might not get killed when launching a new one.
Put the code for starting the Asynctask in onResume. Read the documentation related to activity life cycle.
#Override
public void onResume() {
super.onResume();
Apaptor.refreshListView(Vector);
}

Categories