Button not responding XML issue? - android

I was working yesterday and not today. Have two buttons one to go back, one that is an emergency button that calls a method to open the dialer for a call to the ambulance.
I think it may be an XML problem not sure was working fine now unresponsive maybe I changed something I shouldn't have. When it was clicking earlier it was saying the method could not be found which I just don't get it as it is in the correct file
ReportActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//allows for a smoother transition
overridePendingTransition(0, 0);
setContentView(R.layout.activity_report);
EditText incidentReport = this.findViewById(R.id.Incident_Report);
incidentReport.setSelection(0);
try {
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ReportActivity.this, MainActivity.class);
startActivity(intent);
}
});
} catch (NullPointerException n) {
Toast.makeText(this, "Error,", Toast.LENGTH_LONG);
}
try {
emergency.setOnClickListener(new View.OnClickListener() {
public void onClick(#NonNull View v) {
Intent call = new Intent(ReportActivity.this, CallActivity.class);
startActivity(call);
}
});
} catch (NullPointerException n) {
Toast.makeText(this, "Error,", Toast.LENGTH_LONG);
}
}
#Override
public void onClick(View v) {
}
private boolean checkPermission(String permission) {
int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
return (permissionCheck == PackageManager.PERMISSION_GRANTED);
}
public void call(View view) {
if (checkPermission("android.permission.CALL_PHONE")) {
Intent call = new Intent(Intent.ACTION_DIAL);
String number = "tel:" + getString(R.string.phone_number);
call.setData(Uri.parse(number));
startActivity(call);
}
}
}
activity_report.xml
android:gravity="start"
android:hint="#string/Hint"
android:inputType="text"
android:textAlignment="textStart"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintBottom_toTopOf="#+id/submit_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/submit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="32dp"
android:background="#drawable/yes_button"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Incident_Report" />
<Button
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:background="#drawable/diagnose_button"
android:onClick="onClick"
android:text="Back"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Incident_Report" />
<Button
android:id="#+id/emergency_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="131dp"
android:layout_marginBottom="106dp"
android:background="#drawable/no_button"
android:onClick="call"
android:text="EMERGENCY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>

If you plan on using android:onClick="call"in your xml then you should also have a corresponding function called callin your activity and the logic to perform the click goes inside it.
If you plan on extending the View.onClickListener then inside your override void onClick() you can have a switch statement of your button ids and perform your logic
alternatively you can ignore the first two and get the id of the button and implement the click logic inside the onCreate()

Related

tow button_click_event crushing together

I have tow on_click event in one activity linked to tow different buttons but what happened is when I press the first or second button the first on_click event is the one who is working!! here is the java code:
TextView user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_activity);
user=findViewById(R.id.textView);
Intent i = getIntent();
String msg = i.getStringExtra(("msg"));
user.setText("welcome"+" "+msg+" choose a category");
}
public void science(View v){
Intent i = new Intent(this,science_first_activity.class);
startActivity(i);
}
public void art(View v)
{
Intent i = new Intent(this,art_first_activity.class);
startActivity(i);
}
and here is the xml:
<Button
android:id="#+id/button_s"
android:layout_width="258dp"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="77dp"
android:layout_marginLeft="77dp"
android:layout_marginEnd="76dp"
android:layout_marginRight="76dp"
android:layout_marginBottom="437dp"
android:onClick="science"
android:text="science" />
<Button
android:id="#+id/button_a"
android:layout_width="258dp"
android:layout_height="65dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="77dp"
android:layout_marginLeft="77dp"
android:layout_marginEnd="76dp"
android:layout_marginRight="76dp"
android:layout_marginBottom="328dp"
android:onClick="art"
android:text="art" />
no matter what button I click he will keep go to the science activity!
Start activity if view id are equal to avoid that.
public void science(View v){
if(view.getId==R.id.button_s){
Intent intentS = new Intent(this,science_first_activity.class);
startActivity(intentS );
}}
public void art(View v)
{
if(view.getId==R.id.button_a){
Intent intentA = new Intent(this,art_first_activity.class);
startActivity(intentA );
}}

Keyboard bugs after first letter in EditText

In my first activity when i select the text edit the keyboards appears.
When i write one letter the page scrolls as if the edit text had lost focus (but it didn't) and keyboard stay displayed even if it is not there (if i click approximately on the edit text under the displayed keyboard the keyboard entrance animation start again and everything works). I don't know what to do, look's really like a bug to me. Oh and, the bug only appears on this activity (the first activity to start), Android 8.1 Code and screens :
Edit text before bug
Ghost keyboard (you see it but it is not there, if i click the home button then come back, the keyboard disapeared and i can interract with the button and edit text under)LoginActivity.java:
public class LoginActivity extends AppCompatActivity {
private EditText e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final Button b = findViewById(R.id.startButton);
e = findViewById(R.id.name_input);
//if i click button or enter get to nex activity
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validate();
}
});
e.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
validate();
return true;
}
return false; // i don't know android that much, but i don't think there is a problem here
}
});
//testing purpose
e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Log.e("ERROR", "LOST FOCUS"); //Never called
} else {
Log.e("ERROR", "GOT FOCUS");
}
}
});
//For the ui style, commenting it changes nothing
final View decorView = getWindow().getDecorView();
final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
decorView.setSystemUiVisibility(uiOptions);
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(uiOptions);
}
}
});
}
#Override protected void onPause() {
super.onPause();
}
#Override protected void onResume() {
super.onResume();
}
private void validate() {
if (!e.getText().toString().isEmpty() && e.getText().toString().length() < 16) {
SharedInfos.setName(e.getText().toString());
Log.e("Name : ", e.getText().toString());
Intent activityLaunch = new Intent(this, PresentationActivity.class);
this.startActivity(activityLaunch);
} else {
Toast.makeText(LoginActivity.this, "Name must be not empty and less than 16 char long !", Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml (layout)
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="343dp"
android:layout_height="336dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:contentDescription="Cat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_foreground" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/carter_one"
android:text="#string/app_name"
android:textColor="#color/accent_material_dark_1"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView"
app:layout_constraintEnd_toEndOf="#+id/imageView"
app:layout_constraintStart_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/imageView"
app:layout_constraintVertical_bias="0.9" />
<Button
android:id="#+id/startButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:fontFamily="#font/carter_one"
android:text="#string/app_then"
android:textColor="#color/accent_material_dark_1"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/name_input"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/name_input" />
<EditText
android:id="#+id/name_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:ems="10"
android:fontFamily="#font/carter_one"
android:hint="#string/field_name"
android:inputType="text|textNoSuggestions|textVisiblePassword"
android:textColor="#color/accent_material_dark_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/startButton"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView" />
Found the solution, it was not related to the code or android. I'm using a Xiomi phone and on the internet it is advised to disable the MIUI Optimisation in the dev options when coding. It was the issue, re-enabling it and rebooting the phone solved the problem. Thanks for the answers !

Buttons text gets cleared when i press it

I am currently making an app where i have a list for request to needs to be marked as finished and another list for the items inside the request. If I select a request, the items inside it will show in the items list and buttons with actions to perform for that items. But when I choose another request and press a button, the text on the button gets cleared. I am using sdk 26 on the project. It seems that the text is not displayed but the text value is still there after I tried to see if the text value of the button is still there.
EDIT:
This is the part where I initialize the views.
btnCustomerInfo = findViewById(R.id.btnCustomerInfo);
btnRemoveDiscount = findViewById(R.id.btnRemoveDiscount);
btnClear = findViewById(R.id.btnClear);
btnPay = findViewById(R.id.btnPay);
btnPrintBill = findViewById(R.id.btnPrintBill);
btnUpdate = findViewById(R.id.btnUpdate);
EDIT:
This is the relevant xml code.
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnCustomerInfo"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Customer Info" />
<Button
android:id="#+id/btnRemoveDiscount"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Remove Discount" />
<Button
android:id="#+id/btnClear"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear" />
<Button
android:id="#+id/btnPay"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pay" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnPrintBill"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Print Bill" />
<Button
android:id="#+id/btnTransfer"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Transfer" />
<Button
android:id="#+id/btnMerge"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Merge" />
<Button
android:id="#+id/btnUpdate"
style="#style/AppTheme.Button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Update" />
</TableRow>
EDIT: This is my AppTheme.Button
<style name="AppTheme.Button" parent="AppTheme">
<item name="colorAccent">#ebebea</item>
<item name="android:textColor">#color/black</item>
</style>
I am not setting any value of button in the onClick
EDIT: Button onClicks
btnCustomerInfo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "btnTxt: "+((Button) view).getText());
showCustomerInfoDlg(ip);
}
});
btnRemoveDiscount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
MaterialDialog.Builder mDlgBuild = new MaterialDialog.Builder(ctx);
mDlgBuild.title("Discount");
mDlgBuild.content("Remove discount on " + acOr.getTableName() + " " + acOr.getTableNum() + "(" + acOr.getSessionBundle() + ")" + "?");
mDlgBuild.cancelable(false);
mDlgBuild.positiveText("Ok");
mDlgBuild.negativeText("Cancel");
mDlgBuild.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
dialog.dismiss();
Progress_dialog("Removing discount ...");
progressDlg.show();
}
});
mDlgBuild.onNegative(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
dialog.dismiss();
}
});
mDlgBuild.show();
}
});
for btnTransfer:
final MaterialDialog mDlg = new MaterialDialog.Builder(MainActivity.this)
.customView(R.layout.dlg_transfer_table,true)
.cancelable(false)
.negativeText("Cancel")
.onNegative(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
dialog.dismiss();
}
}).show();
TextView lblTableNum = (TextView)mDlg.findViewById(R.id.lblDlgTransferTableNumber);
TextView lblCustomerName = (TextView)mDlg.findViewById(R.id.lblDlgTransferCustomerName);
RecyclerView lstTable = (RecyclerView)mDlg.findViewById(R.id.lstDlgTransferTableList);
The other buttons do printing, opens a webview, remove items on the list for the items inside the request, and shows another button. Even if the buttons still doesn't have code in it, clicking it still removes the text on the buttons display.
EDIT: Gif
EDIT: this are the code of the two buttons
Pay button:
final MaterialDialog mDlg = new MaterialDialog.Builder(MainActivity.this)
.title("Payment Options")
.customView(R.layout.dlg_pay_option,true)
.cancelable(false).show();
Button btnCash = (Button)mDlg.findViewById(R.id.btnDlgPayOptionCash);
Button btnCard = (Button)mDlg.findViewById(R.id.btnDlgPayOptionCard);
Button btnOther = (Button)mDlg.findViewById(R.id.btnDlgPayOptionOther);
Button btnCancel = (Button)mDlg.findViewById(R.id.btnDlgPayOptionCancel);
Button btnRemovePayment = (Button)mDlg.findViewById(R.id.btnDlgPayOptionRemovePayment);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDlg.dismiss();
}
});
btnCash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDlg.dismiss();
typeOfPayment = "cash";
CashPayment();
}
});
btnCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDlg.dismiss();
typeOfPayment = "card";
CardPayment();
}
});
btnOther.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDlg.dismiss();
typeOfPayment = "other";
OtherPayment();
}
});
Update button:
Intent i = new Intent(MainActivity.this, UpdateOrder.class);
i.putExtra("or_id", acOr.getSessionBundle());
i.putExtra("cust_name", acOr.getCustomerName());
i.putExtra("table_num", acOr.getTableNum());
startActivity(i);
finish();
At first, I already selected an item on the right then clicked pay. Next, I selected another item on the right and clicked pay. Then I clicked cancel on the dialog. Lastly, I clicked the update button.
There's a good chance that the text is not cleared but has the background color in a certain state. To deal with it, you can create a color state list and set it in the android:textColor attribute.

Backpreesing takes me to previous activity

I know this question has been asked many times but hear me out. I have MainActivity which is a LoginActivity & it uses facebook login and AccountKit From Facebook to login and start next activity which is a ContactsActivity. When i am in ContactsActivity and press back button it takes me to MainActivity but i want to close the application as user has successfully logined in and if the user again opens the app it should open ContactsActivity directly as user has signed in successfully previously.
Problem is when i press Back button in Contacts Activity,it takes me to LoginActivity,i already tried finish() after startActivity(intent) which closes my app and not takes me to ContactsActivity after successfull signin.If i open my app again then it takes me directly to ContactsActivity.
I even tried onBackPressed in which i wrote finish but it doesnt work.
In Android Manifest also i wrote android:noHistory="true" in MainActivity tag but it doesn't work.
Please help
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="pritish.sawant.com.simplychat.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/loginwithphonenumber"
android:text="#string/loginwithphoneno"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:textColor="#android:color/white"
android:background="#drawable/login_button_selector"
android:textSize="16sp"
android:layout_centerInParent="true"
android:textAlignment="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/email_login_button"
android:text="#string/loginwithemail"
android:layout_below="#+id/loginwithphonenumber"
style="#style/Base.Widget.AppCompat.Button.Borderless"
android:textColor="#android:color/white"
android:background="#drawable/login_button_selector"
android:textSize="16sp"
android:textAlignment="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
/>
<com.facebook.login.widget.LoginButton
android:id="#+id/facebook_login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/email_login_button"
android:textSize="16sp"
android:textAllCaps="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
/>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
public static int APP_REQUEST_CODE=1;
private LoginButton facebookloginButton;
private Button emailloginbutton,phonenologinbutton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookloginButton=(LoginButton)findViewById(R.id.facebook_login_button);
emailloginbutton=(Button)findViewById(R.id.email_login_button);
phonenologinbutton=(Button)findViewById(R.id.loginwithphonenumber);
callbackManager=CallbackManager.Factory.create();
facebookloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
launchContactsActivity();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), R.string.loginfailed,Toast.LENGTH_LONG).show();
}
});
AccessToken accessToken= AccountKit.getCurrentAccessToken();
com.facebook.AccessToken loginToken= com.facebook.AccessToken.getCurrentAccessToken();
if(accessToken!=null || loginToken!=null){
launchContactsActivity();
}
emailloginbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onLogin(LoginType.EMAIL);
}
});
phonenologinbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onLogin(LoginType.PHONE);
}
});
}
private void launchContactsActivity(){
Intent intent=new Intent(getApplicationContext(),ContactsActivity.class);
startActivity(intent);
finish();
}
private void onLogin(final LoginType loginType) {
// create intent for the Account Kit activity
final Intent intent = new Intent(this, AccountKitActivity.class);
// configure login type and response type
AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
new AccountKitConfiguration.AccountKitConfigurationBuilder(
loginType,
AccountKitActivity.ResponseType.TOKEN
);
final AccountKitConfiguration configuration = configurationBuilder.build();
// launch the Account Kit activity
intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configuration);
startActivityForResult(intent, APP_REQUEST_CODE);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
if(requestCode==APP_REQUEST_CODE){
AccountKitLoginResult accountKitLoginResult=data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
if(accountKitLoginResult.getError()!=null){
String toastMessage = accountKitLoginResult.getError().getErrorType().getMessage();
Toast.makeText(this, toastMessage, Toast.LENGTH_LONG).show();
} else if (accountKitLoginResult.getAccessToken() != null) {
launchContactsActivity();
}
}
}
}
In normal way you shouldn't close app - user will moves it to back by pressing home or closing your activities by pressing back.
But you can use this tips:
1) Close old activities after starting the new ones if they not needed in back stack:
startActivity(newActivityIntent);
finish();
The override onBackPressed and the app will close
2) You can move task to back (instead of close) if you need http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)

start Activity B(called by onCreate of an Application Class's sub Class) on Top of Activity A(called by onResume of an Activity Class's sub Class)

I have 2 activities that involved in this question:
LoginActivity: A Login in Screen (called by a subclass of Activity Class)
Updater Activity: A Update Screen that show updating progress when new app version is availble (called by a subclass of Application Class)
This is what I want:
When the app FIRST LAUNCH, and if new version of the app is available, the UPDATER ACTIVITY should be on top of every activity include the Login Activity,
so it will be the first screen user see during first launch of the app.
only after the updating progress is done, the UPDATER ACTIVITY will be dismissed then Login Screen be on top.
However this is what actually happened:
The LOGIN ACTIVITY always on top of UPDATER ACTIVITY...
My Guess is that since the UPDATER ACTIVITY is called By APPLICATION CLASS's subClass, and the LOGINACTIVITY is called by an ACTIVITY CLASS sub class,
the Application class's method such as OnResume always get called before Activity onCreate Method, thus the LOGINACTIVITY always appear on top of UPDATER ACTIVITY,
I just wondering if there is anyway I could make my UPDATER ACTIVITY be on top of any activities including the LOGIN ACTIVITY?
//LoginActivity involved
public abstract ClassA extends Activity {
.....
.....
protected void onResume() {
super.onResume();
.....
if(isAuthenticated){
Intent intent = new Intent(this, LoginActivity.class)
.putExtra(...)
startActivityForResult(intent, XXX);
}
}
}
====
//Updater Activity involved
public abstract class ClassB extends Application {
....
....
....
public void onCreate() {
super.onCreate();
Intent updaterIntent = new Intent(this, UpdaterActivity.class);
updaterIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(updaterIntent);
}
}
i have diffrent solution instent of activity create dialog box activity.
public class ViewDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.updatedialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogUpdate = (Button) dialog.findViewById(R.id.update);
Button dialogcancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogremindme = (Button) dialog.findViewById(R.id.remindeme);
TextView textView= (TextView) dialog.findViewById(R.id.machhint1);
TextView textView2= (TextView) dialog.findViewById(R.id.machhint2);
textView2.setText("Your old version is : "+versionName);
textView.setText("Current version in playstore : "+version);
dialogcancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
SharedPreferences.Editor editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
editoradddetail1.putString("days", "");
editoradddetail1.commit();
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
later="";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
dialog.dismiss();
}
});
dialogremindme.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editoradddetail1 = getSharedPreferences("later", MODE_PRIVATE).edit();
Date someDate = new Date(); // Or whatever
Date dayAfter = new Date(someDate.getTime() + TimeUnit.DAYS.toMillis( 2 ));
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
String formattedDate=dateFormat.format(dayAfter);
editoradddetail1.putString("days", formattedDate);
editoradddetail1.commit();
dialog.dismiss();
}
});
dialog.show();
}
}
//and xml is.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="250dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="250dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/main"
android:background="#FFed6904"
android:layout_height="120dp">
<TextView
android:id="#+id/machhint"
android:text="Note:- Your current Version Did Not Match"
android:layout_width="match_parent"
android:textColor="#color/white"
android:textSize="13dp"
android:layout_height="60dp" />
<TextView
android:id="#+id/machhint1"
android:text="preschool version is"
android:layout_width="wrap_content"
android:textSize="13dp"
android:textColor="#color/white"
android:layout_below="#+id/machhint"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/machhint2"
android:text="Your Vertion is :"
android:layout_width="wrap_content"
android:textColor="#color/white"
android:textSize="13dp"
android:layout_below="#+id/machhint1"
android:layout_height="30dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/text1"
android:layout_below="#+id/main"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXTO"
android:id="#+id/text_dialog"
android:layout_marginTop="3dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="3dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_below="#+id/text1"
android:gravity="center_vertical|center_horizontal"
android:layout_height="55dp">
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Update"
android:id="#+id/update"
android:gravity="center"
android:layout_toLeftOf="#+id/remindeme"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Later"
android:id="#+id/remindeme"
android:gravity="center"
android:layout_centerHorizontal="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="45dp"
android:text="Cancel"
android:id="#+id/cancel"
android:gravity="center"
android:layout_toRightOf="#+id/remindeme"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>

Categories

Resources