onbackpressed() method crash - android

I am using authentication to enter into a page, after authenticated only the user enters into the page. i wrote a code for onbackpressed(), but it is not working. Here DatabaseDemo and Login are the two classes. when i press the back button the login class with username and password is displaying.
public class DatabaseDemo extends TabActivity {
DatabaseHelper dbHelper;
GridView grid;
TextView txtTest;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SetupTabs();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(1, 1, 1, "Add Employee");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
//Add employee
case 1:
Intent addIntent=new Intent(this,AddEmployee.class);
startActivity(addIntent);
break;
}
super.onOptionsItemSelected(item);
return false;
}
void SetupTabs()
{
TabHost host=getTabHost();
TabHost.TabSpec spec=host.newTabSpec("tag1");
Intent in1=new Intent(this, AddEmployee.class);
spec.setIndicator("Add Employee");
spec.setContent(in1);
TabHost.TabSpec spec2=host.newTabSpec("tag2");
Intent in2=new Intent(this, GridList.class);
spec2.setIndicator("Employees");
spec2.setContent(in2);
host.addTab(spec);
host.addTab(spec2);
}
#Override
public void onBackPressed()
{
Intent i = new Intent(DatabaseDemo.this, Login.class);
startActivity(i);
}
}

You have it to start the Login on back pressed, so if it's crashing, then there is something wrong with your login activity more than likely, and not here.

Related

Base Activity for custom ListView menu

I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.

Multiple Activities and Up Button

I have an activity that can be asked to run after clicking buttons on many different activities and hence it does not have a "single parent". Therefore in the android manifest I cannot define its parent so I cant get the "Up" button to function properly.
Is there a way I can have the "up" button return to the activity that called it?
You can pass ComponentName of starting activity as an extra
intent = new Intent(this, UpButtonActivity.class);
intent.putExtra(EXTRA_PARENT_COMPONENT_NAME, new ComponentName(this, ThisActivity.class));
startActivity(intent);
The Activity with up button
private ComponentName parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = getIntent().getParcelable(EXTRA_PARENT_COMPONENT_NAME);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getId()) {
case android.R.id.home:
if (parent != null) {
final Intent parentIntent = new Intent();
parentIntent.setComponentName(parent);
startActivity(parentIntent);
finish();
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}
//...
}
}

getIntent is not working inside MainActivity. Getting weird behaviour

I have one MainActivity and I have defined below code in onCreate() method. The intention is, when MainActivity gets extra String "EXIT" then show Toast message:
Intent current = getIntent();
if (current !=null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
This MainActivity starts another activity "DayOne" on some button press like:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
MainActivity.this.startActivity(i);
}
Now I am returning back from "DayOne" to MainActivity after putting extra string "EXIT". This I am doing inside onOptionsItemSelected(MenuItem item) method:
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
The issue is, when MainActivity is getting called from DayOne with extra string "EXIT"; I am not seeing the Toast message defined in MainActivity. What is missing or wrong here?
Appreciate any help.
Thanks all for your comments and helps.
I have figured out the issue here. It was because the manifest file had entry an entry android:launchMode="singleInstance" for Both the activities (MainActivity and DayOne Activity)..
Removing it from there, it worked fine.
First check your null then get the Intent String, can you just make your code look like below within in onCreate of MainActivity#
Bundle extra= getIntent().getExtras();
if(extra!=null){
String _StrExit=extra.getString("EXIT");
if(_StrExit.equalsIgnoreCase("EXIT")){
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
Update
Make change while calling Intent from menuitem
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent i = new Intent(DayOne.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
String _Str="EXIT";
i.putExtra("EXIT", _Str);
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
Its working for me.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent current = getIntent();
if (current != null && current.getStringExtra("EXIT") != null) {
Toast.makeText(this, "exiting", Toast.LENGTH_LONG).show();
}
}
public void processGo(View view) {
Intent i = new Intent(MainActivity.this, OneDayActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
OneDayActivity:
public class OneDayActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_one);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.day_one, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Exit")) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("EXIT", "EXIT");
startActivity(i);
finish();
}
return super.onOptionsItemSelected(item);
}
}
dont clear the stack instead of if you want to do some task when you come back to your main activity (using "EXIT" string) you can achieve that using onActivityResult also.:
public void processGo(View v){
Intent i = new Intent(MainActivity.this,DayOne.class);
MainActivity.this.startActivity(i,10); // 10 is the id to handle
}
#Override
public void onActivityResult( int requestCode, int resultCode, Intent data )
{
super.onActivityResult( requestCode, resultCode, data );
switch ( requestCode )
{
case ( 10 ): // id that we pass on start activity
{
if ( resultCode == Activity.RESULT_OK )
{
Toast.makeText(this, "exiting"+data.getStringExtra( "EXIT", "" ), Toast.LENGTH_LONG).show();
}
}
break;
}
}
On finish
public boolean onOptionsItemSelected(MenuItem item){
if(item.getTitle().equals("Exit")){
Intent resultIntent = new Intent();
resultIntent.putExtra( "EXIT", "EXIT" );
setResult( Activity.RESULT_OK, resultIntent );
finish();
}
return super.onOptionsItemSelected(item);
}
on finish it will go back to Main Activity and call onActivityResult automatically where you can do your task.
Sorry for the typo hope it will help.

Return Button - unreachable

I want to create a button, which closes the current activity. Like a "return" button.
Here are code fragments I tried:
Here is the full .java:
public class OtherApps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_apps);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.otherappsmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.previous:
finish();
break;
case R.id.home:
Context context = getApplicationContext();
CharSequence text = "Activitys are not closed!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
break;
case R.id.exit:
finish();
System.exit(0);
case R.id.help:
String url = "http://www.google.de/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
final Button OtherApps = (Button)findViewById(R.id.previousbutton);
OtherApps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
return true;
}
}
But Eclipse says "the first line is unreachable".
Does anyone know what's the error?
Thanks for help!
If your "first line" is unreachable, then it's more important to know, what the code before these two versions is. It might be, that you have a return statement there or a condition that is always false.
In this case the code for attaching the on-click listeners will never be reached.
p.s.
You have two lines wehre you return from the method what means that the following code is never executed:
switch(item.getItemId()) {
...
default: // here, return if none of the values above matched
return super.onOptionsItemSelected(item);
}
return true; // here, return always
// conclusion: this gets never executed: Eclipse says "line not reachable"
final Button OtherApps = (Button ...
This code should work (the first example should be preferred).
The error you are getting sounds as if you have a return-statement anywhere inside that method BEFORE you have the pasted code. Search for that, it should fix the error.
EDIT:
public class OtherApps extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_apps);
final Button OtherApps = (Button) findViewById(R.id.previousbutton);
OtherApps.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}

Android: show menu options on an Activity with Tabs

I want to show menu options in an activity which hosts tab views. Here is the code of my tab view activity.
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("photo list")
.setContent(new Intent(this, List8.class)));
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.removeGroup(0);
menu.add(0, 0, 0, "Home").setIcon(
android.R.drawable.ic_menu_preferences);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0:
setResult(10);
finish();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
Now when i am pressing android menu button, onPrepareOptionsMenu is called which is correct but when i select the menu option, nothing happens. I have also debugged my code and control is not reaching in onMenuItemSelected.
Please help.
I got my solution.. i dont whether it is perfect way to do it but it is working..
instead of using onMenuItemSelected , i just used onOptionsItemSelected and my code is working.
Here is the final code:
public class Tabs3 extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("list")
.setContent(new Intent(this, List1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("photo list")
.setContent(new Intent(this, List8.class)));
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.removeGroup(0);
menu.add(0, 0, 0, "Home").setIcon(
android.R.drawable.ic_menu_preferences);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
setResult(10);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}

Categories

Resources