Add a back button in activity - java

Good morning, Guys I have a code that plays an audio when the activity starts it performs a welcome greeting, however when I go to other activities and return to the main menu the audio is played again I would like to notice that when I use the back action bar button I don't have this problem, but I need to use a button in the fragment or activity, I can't have an action bar in my app
Code to Play audio:
new Timer().schedule(new TimerTask() {
#Override
public void run() {
MediaPlayer play= MediaPlayer.create(MainActivity.this,R.raw.audioboatarde);
play.start();
}
}, 1000);
Code button on fragment:
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1 = new Intent(getContext(), MainActivity.class);
startActivity(intent1);
}
});

Stop your media playback when you leave the activity in onPause of the Activity
#Override
public void onPause() {
super.onPause();
play.stop();
}
And you don't need to restart the previous activity on click of the button in the second activity, you just need to call onBackPressed() to go to the previous activity.
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});

This is my second answer.
In the case you have to relauch the first activity(if it is destroyed) from the second one,
you can also use onSaveInstanceState(Bundle outState)
to save the audio played state in the activity.
In the first activity
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean("isPlayed",true);
super.onSaveInstanceState(outState);
}
In onCreate() of the first activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && savedInstaceState.getBoolean("isPlayed") == false) {
new Timer().schedule(new TimerTask() {
#Override
public void run() {
MediaPlayer play= MediaPlayer.create(MainActivity.this,R.raw.audioboatarde);
play.start();
}
}, 1000);
}
}
Later on, you can relaunch the first activity from the second activity.
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1 = new Intent(getContext(), MainActivity.class);
startActivity(intent1);
}
});
Dont forget to stop the audio playback in the first activity.
#Override
public void onPause() {
super.onPause();
play.stop();
}

Related

How to stop looping in activites and at the same handle the default on screen navigation back?

ImageView Back = findViewById(R.id.imageView11);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Nav.class);
startActivity(intent);
finish();
}
});
Button cash = findViewById(R.id.button11);
cash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Cash.class);
startActivity(intent);
finish();
}
});
TextView Card = findViewById(R.id.textView26);
Card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Card.class);
startActivity(intent);
finish();
}
});
Button AddCard = findViewById(R.id.button12);
AddCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, AddedCard.class);
startActivity(intent);
finish();
}
});
TextView promo = findViewById(R.id.textView28);
promo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, promocode.class);
startActivity(intent);
finish();
}
});
Button Voucher = findViewById(R.id.button13);
Voucher.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Vouchers.class);
startActivity(intent);
finish();
}
});
}
}
If i remove finish(); this create loops when I press default navigation back button.
like if I visited promocode class,then voucher class.from vouchar class I want to go back in Main Class by pressing default navigation back button.it will reverse like vouchar to promode then main class.
But I want by default back button will bring me vouchar class to directly main class.
But after adding finish(); whenever i press navigation back button the app quits.
I want a solution that...default navigation back button will act like my every customized back button for every each individuals frames !!
The key is to provide proper navigation.
Here's the Official Documentation to understand the tasks and back stack.
The other way to provide back navigation to the custom activity is to override onBackPressed() method of your activity like :
#Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(YourCurrentActivity.this, MainActivity.class));
finish();
}

Why doubleclick on a custom button crashes an android app

I implemented a custom button and added a task to it with delay so it shows the animation.
When I double click it, it crashes. I want to make so it's only clickable once.
i have tried setEnabled(false);
i have tried setClickable(false);
i tried a variable that check if a button has been clicked and disables it.
public class Login extends AppCompatActivity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
}
}
As I wrote, I want that if the button has been clicked that it becomes unclickable.
Try this:
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final SubmitButton LoginBtn = findViewById(R.id.login);
handler = new Handler();
LoginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginBtn.setEnabled(false);
handler.postDelayed(new Runnable() {
#Override
public void run() {
LoginBtn.setEnabled(true);
Intent startActivity = new Intent(Login.this, Main_page.class);
startActivity(startActivity);
finish();
}
}, 3200);
}
});
...
Explanation:
Your enable/disable block is outside of the onClick listener and it's in the onCreate method: in this way you call setEnable() method only when the activity was created.
But some time setEnable() can not work in case of very rapid click like explained in the second response here. In that case you can use a timer to check the elapsed time.
By the way, I think that your app crash because you don't handle in the right way the Handler. I suggest you also to add:
#Override
protected void onStop(){
handler.removeCallbacksAndMessages(null);
}
Try...
view.setOnClickListener(null);
...after your click event.

How to end a handler and change activity with a button?

Hello I am a college student with a background of html and JavaScript making this app as a side project for work. I would like help with ending a count down timer early via a button press while also taking the user to another activity. I have used two different guides to assemble my code and I have the timer working. I have a limited knowledge of java so any help would be welcomed, here is my code:
public class Main2Activity extends AppCompatActivity {
Button button1;
int flag=0;
// page should show up for 2 seconds then take you to the main page, but I want a button there to stop the count down and take the user to a different page That I will use as a credits page
private static int TIME_OUT = 2000; //Time to launch the another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Main2Activity.this, MainActivity.class); //Main2Activity is the Welcome page and MainActivity is the home page
startActivity(i);
finish();
}
}, TIME_OUT);
button1 = (Button) findViewById(R.id.credit);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
flag = 1;
Handler.removeCallbacksAndMessages(null);
Intent intent = new Intent(Main2Activity.this, credit.class);
startActivity(intent);
}
});
}
}
You should declare a global Handler and use it everywhere. Don't use final in Handler
Handler handler;
private static int TIME_OUT = 2000; //Time to launch the another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Main2Activity.this, MainActivity.class); //Main2Activity is the Welcome page and MainActivity is the home page
startActivity(i);
finish();
}
}, TIME_OUT);
button1 = (Button) findViewById(R.id.credit);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
flag = 1;
handler.removeCallbacksAndMessages(null);
Intent intent = new Intent(Main2Activity.this, credit.class);
startActivity(intent);
}
});
}
You can use sendMessage+removeMessage to CANCEL a post but you cant execute it immediately.
Handler h = new Handler() {
public void handleMessage(int what){
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
finish();
}
}
h.sendEmptyMessageDelayed(111, TIME_OUT);
//On your button Listener
h.removeMessages(111);
This way the message will be canceled and not fired in the timeout.

How to launch google calendar from the app I'm developing

I'm developing an APP for a schoolwork. When I launch my app, I configured 4 buttons, one of them is a calendar.
What I want is when I click on Calendar, this open the Google calendar; or if I don't have installed it, take you to Google Play.
The other buttons are activities in the same application, but these are solved.
The main activity:
public class MainActivity extends Activity {
#Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
entrarboton();
}
private void entrarboton() {
ImageButton accionentrar = (ImageButton) findViewById(R.id.imageButton0);
accionentrar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Calendari.class));
}
});
ImageButton accionentrar2 = (ImageButton)findViewById(R.id.imageButton3);
accionentrar2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Notes.class));
}
});
ImageButton accionentrar3 = (ImageButton)findViewById(R.id.imageButton2);
accionentrar3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Apunts.class));
}
});
ImageButton accionentrar4 = (ImageButton)findViewById(R.id.imageButton4);
accionentrar4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Altres.class));
}
});
}
}

button click event doesnt work

I'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}

Categories

Resources