I created a custom dialog for my main activity with two buttons, Exit and Continue:
public class AgeConfirmationDialog extends Dialog {
public AgeConfirmationDialog(Activity a) {
super(a);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCancelable(false);
setContentView(R.layout.age_dialog);
// .....
// Find the View objects; checkboxes and buttons logic; SharedPreferences
// .....
}
// .....
}
This is how the dialog is launched from the MainActivity:
AgeConfirmationDialog d = new AgeConfirmationDialog(this);
d.show();
This custom dialog pops out every time the main activity is started, and asks for a age confirmation. I don't want the users to close this dialog using the back button, so I added setCancelable(false) in the onCreate method. The Continue button is disabled until a checkbox is checked. If the Continue button is pressed, the dialog is dismissed - using setOnClickListener.
The problem is that I don't know how to dismiss that custom dialog AND finish the main activity when the Exit button is pressed.
Is it possible to do this from the AgeConfirmationDialog class by setting a View.OnClickListener on the Exit button?
change the code to something like this:
public class AgeConfirmationDialog extends Dialog {
Activity mainActivity;
public AgeConfirmationDialog(Activity a) {
super(a);
this.mainActivity = a;
}
//in onClick method of finish-button
public void onFinishClick(View v) {
mainActivity.finish(); //finish activity
}
}
I don't know how to dismiss that custom dialog AND finish the main
activity when the Exit button is pressed.
Then you just have to put in your ExitButton Click Listener:
finish();
For example:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Exit", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).create().show();
Do something in your onclicklistener of exit button. Like:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
And your MainActivity do something like:
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
What you basically do here is going to mainactivity which is your start activity with some key/value extra and check it in your mainactivity through getintent.
This is best process to exit application or you can use it also in logout.
Related
i am working on an app in which, button Onclick dynamically created a button from one activity and button is appeared in another activity.
So, for your button, see the code below. I'm just using SharedPreferences for testing if button has been clicked before or not.
// Inside your onCreate() method of your SecondActivity.java
((Button)findViewById(R.id.activity2_button)).setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).edit().putBoolean("ShowButton", true).commit(); // put Boolean inside SharedPreferences
Intent main = new Intent(this, FirstActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(main);
finish();
}
}
And now, the FirstActivity.java code :
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
if (getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).getBoolean("ShowButton", false))
((Button)findViewById(R.id.activity1_button)).setVisibility(View.VISIBLE);
else
((Button)findViewById(R.id.activity1_button)).setVisibility(View.GONE);
}
}
Test it, and tell me if this works great. Hope it will work for you, Darkball60 :)
I am writing an application which is having an activity and I am launching a pop up ( pop up I have implemented in another class which is not an activity).
In my main activity I have code to display the popup when there is an event. I am successfully able to do that. But I need to cancel the popup when I get another event. ( I will get this event only in the main activity).
How can I finish the popup from the main activity when I receive the event?
create a public method in the class where you have created the popUP(dialog) and write inside it as
public void dismissDialog(){
dialog.dismiss();
}
then whenever you want to remove the dialog call this method...
If this pop up is a dialog you can do it with
dialog.cancel();
final AlertDialog.Builder aBuilder;
//constructor(Context c){//mContext = c;}
public void showMessage(final String title, final String s) {
aBuilder = new AlertDialog.Builder(mContext);
aBuilder.setTitle(title);
aBuilder.setIcon(R.drawable.ic_launcher);
// aBuilder.setIcon(R.drawable.icon);
aBuilder.setMessage(s);
}
public void dismissMessage() {
aBuilder.dismiss();
}
When you press the back button while in an activity, by default, does the application not go back to the activity that called it? I am calling an activity in my application(call it Activity B), from Activity A, but when I hit the back button while in Activity B, I am taken back to the main page of the application.
So I guess in general, does pushing the back button on your phone take you to the calling activity?
Calling activity B from within an inner class of activity A:
class HeadlineButtonListener implements OnClickListener {
private Story story;
public HeadlineButtonListener(Story story) {
this.story = story;
}
#Override
public void onClick(View v) {
Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
myIntent.putExtra(Constants.STORY_EXTRA, story);
HeadlineBoard.this.startActivity(myIntent);
finish();
}
}
You call finish() on first activity after firing the next activity, this will cause it to be removed from the activity stack, just remove the call to finish():
#Override
public void onClick(View v) {
Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
myIntent.putExtra(Constants.STORY_EXTRA, story);
startActivity(myIntent);
}
I am developing an Android app, this app has a dozen of Activities, each one is for a corresponding screen. Now I have this common subtitle bar on top of the screens.
this subtitle bar has a button to display an alert dialog which shows link list to go to a different screen.
I could write a same function for each activity to call the alert dialog, but that would be tedious if I want to modify them, so I created this class:
public class MenuAlertDialog extends Activity {
/*
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
*/
public void createMenu(final Context context){
AlertDialog.Builder dlg = new AlertDialog.Builder(context);
dlg.setTitle("menu");
String[] items = {"pageA", "pageB", "pageC", "pageD", "pageE"};
dlg.setItems(items, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which){
switch(which){
case 0:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
break;
default:
break;
}
}
});
dlg.show();
}
}
and call it from each activity, like this:
MenuAlertDialog menu = new MenuAlertDialog();
menu.createMenu(this);
from inside of onCreate.
It can display the alertDialog, but whenever I press pageA link, it fails with an unexpected error.
Logcat says its a nullpointererror and the cause seems
startActivity(intent);
What am I doing wrong?
Remove the code
extends Activity
as you have no need to extend your class that you are creating since it does not rely on any activity related functionality.
Where you call startActivity(intent); replace it with
context.startActivity(intent);
You should change the class to Extends Dialog and not activity.
Also for Try this:
Check out this tutorial on how to create a custom dialog. Custom Dialog
Also Here Another Tutorial
And Here
I have my main class setup and a worker thread, one of my early requests that I make in run() is to call my second class called login. I do this like so:
login cLogin = new login();
cLogin.myLogin();
class login looks like this:
package timer.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class login extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
}
public void myLogin() {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage(this.getString(R.string.intro));
// add a neutral button to the alert box and assign a click listener
alertbox.setNeutralButton("Register New Account",
new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
}
});
// add a neutral button to the alert box and assign a click listener
alertbox.setNegativeButton("Login",
new DialogInterface.OnClickListener() {
// click listener on the alert box
public void onClick(DialogInterface arg0, int arg1) {
// the button was clicked
}
});
// show it
alertbox.show();
}
10-01 14:33:33.028: ERROR/AndroidRuntime(440):
java.lang.RuntimeException: Unable to start activity ComponentInfo{timer.test/timer.test.menu}:
java.lang.IllegalStateException:
System services not available to Activities before onCreate()
I put onCreate in but still having the same problem. How can I fix this?
You have a couple of options:
1) Move your public void myLogin() {..} to your main activity. I recommend this solution since you don't need another activity for your purposes.
2) Call startActivity on your login class before calling the myLogin(). Since you inherite from Activity onCreate needs to be called before you call anything else. That's why you get the exception. startActivity is called like this:
Intent i = new Intent(this, login.class);
startActivity(i);
You cannot do it this way simply because you are using the context
AlertDialog.Builder alertbox = new AlertDialog.Builder(this); //this is the activity context passed in..
The context is not available until Activity onCreate is called via startActivity. and not by constructing an new instance of the login object, you can try passing in an context from the activity calling this method
public void myLogin(Context context) {
// prepare the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
//blah blah blah...
}
yes and never construct an activity instance via the constructor.. -.-