I'm building an app with a navigation drawer, and an action bar with a button which pressed should take users to a log in page. The log in page is an activity.
I have inserted what I believe to be the appropriate code, however the section in bold comes back with the following error: Annotations are not allowed here
This code previously worked, and now it has this error and my app obviously wont launch. Does anyone know why? Thanks!
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_LoginPage) {
Toast.makeText(getApplicationContext(), "Welcome to the Employee Log-in Page", Toast.LENGTH_LONG).show();
switch (item.getItemId()){
case R.id.action_LoginPage:
Intent intent = new Intent(this, LoginPageMain.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
**#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Code for Nav Bar Item Clicks (Button Clicks)
int id = item.getItemId();**
When i logout my app (press logout menu button) from second activity, then it redirect to login page but when i pressed back button again show second screen Activity, it should be goto main screen on back pressed, because i use finish() method but still not go to main screen. then what i do?
Here is my code.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second_adapter, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.logout_sec_Act) {
session.logoutUser();
finish();
Toast.makeText(Second_activity.this, "Logout...", Toast.LENGTH_LONG)
.show();
}
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
That is because your previous activity is still in the back stack of your task. One way to solve it is by moving to a new empty task when you logout, something like this:
Intent logoutIntent = new Intent(this, MainActivity.class);
logoutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(logoutIntent);
How does this.C() work exactly? method C is non-static so it has to be called via an instance. this refers to the current object but what object and how is it created? I didn't explicitly create an object so does compiling implicitly creates one behind the scene?...
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.C();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if(id ==R.id.action_search){
this.openSearch();
}
else if (id == R.id.action_settings) {
// openSettings();
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message= editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void C(){
}
public void openSearch(){
getActionBar().hide();
}
}
For Android when you start an activity for the first time, the onCreate is called by the Android OS. This is where you should write code to initialize all your views and variable. In the case of this.c() -or just c() - Android will automatically call it since it is in the onCreate method. Also since c() is part of the class, the instance of the class at run time is referred by "this" implicitly.
I recommend understanding life cycle of android apps. it will help you with developing good apps. http://developer.android.com/training/basics/activity-lifecycle/starting.html
I hope I understand your question correctly.
In the onCreate method you already have a this pointer, it is given when it is called. It is not sure where the caller gets it from, and it does not matter anyway.
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...
Working with a Xoom Tablet and the menu(options) button on the bottom of the screen does not light up (is not active).
Any suggestions?
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.option_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.about:
about();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void about() {
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("About");
alertDialog.setMessage("App v1.0");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}
The menu key shown on the system bar in Android 3.0+ is a compatibility feature for running older apps. Setting targetSdkVersion="10" means you are not developing an app that targets Android 3.0+ and the system will adjust compatibility behavior for your app accordingly.
If you are truly writing an app to run on Android 3.0+ tablets you will not have a menu key on the system bar. Forget about it. Put it out of your mind. :) Abusing compatibility features in this way explicitly breaks Android UI design guidelines. The action bar will present your activity's options menu if present. If you do not have an action bar in your activity you should present options using some other on-screen affordance.
I figured it out..
My target and min Sdk was:
android:targetSdkVersion="11"
android:minSdkVersion="11"
Changed to:
android:targetSdkVersion="11"
android:minSdkVersion="10"
Menu button doesn't work on 11 and up.