How to open TabActivity - android

In my app, I want it to go to my main activity when user pushes hardware menu button options. But my main activity is a TabActivity. You will understand it better if you see the code.
The problem is I made a menu item which should start the main activity when user a clicks it. But I get an error when I click on it and forces to close. Can anyone help?
Here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menuhome:
Intent i = new Intent("com.eran.exampleapp.MYMAINACTIVITY");
startActivity(i);
break;
}
return false;
}
and my main activity:
public class MyMainActivity extends TabActivity

First of all... why are you using upper case like this: MYMAINACTIVITY? Isn't your activity called MyMainActivity? Does it make sense for you?
Try this:
Intent i = new Intent(this, MyMainActivity.class);
startActivity(i);
return true;

Related

returning back to main activity from SettingActivity

I got SettingActivity from AndroidStudio Gallery, which uses ActionBar. Everything is fine when I click back button from ActionBar inside SettingsActivity. But when I want to return to main activity using this button in ActionBar nothing happens.
My SettingsActivity uses headers for preferences, so I thought I can check whether I am "inside" any of these headers, and if answer is none then I am in main setting screen and I can call main activity using startActivity(this,MainActivity.class). But problem is I cann't determine whether I am in start screen or in some header. Of course if there are more easy ways to do it I would appreciate it very much.
to make action bar back press return back , you have to do it in menu item selected
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
Intent homeIntent = new Intent(this, HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return (super.onOptionsItemSelected(menuItem));
}

Sub activity disables Bluetooth Adapter

I have added an onDestroy in my Main activity
#Override
protected void onDestroy() {
if (BTAdapter.isEnabled()) {
BTAdapter.disable();
}
this.unregisterReceiver(receiver);
super.onDestroy();
}
In my Main Activity I start another activity through Navigation Drawer
if (id == R.id.nav_devices) {
intent = new Intent("com.navigationwithrssi.RSSIActivity");
startActivity(intent);
}
But when I come back to my Main Activity (using the default back button in Toolbar), the RSSIActivity automatically disables Bluetooth Adapter.
I just want my Main Activity to be able to do that, is there any way to do that?
I got my problem fixed. It was actually pretty easy, on the tutorial about Providing Up Navigation on developer.android.com
I just put this code in my onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Also I made the launch mode of my Parent Activity as <singleTop>
android:launchMode="singleTop"
What it does is rather than creating a new instance of your parent activity, it just brings it to the top of the stack.

Action bar back button from un activity to previous activity fragment

I'm using a navigation drawer in a main activity with many fragments
For example :
I have fragment 1 for presentation
And fragment2 that contains a listView.
If I click an item in the listView it will launch another activity
When i click the action bar back button i want to go to the main activity fragment2
But the fragment1 is used
How can I use the second fragment
Thanks for your answers
In your second activity's onCreate, put(although i think you already have) :
getActionBar().setDisplayHomeAsUpEnabled(true);
and inside second activity's options handling(i think you've done this too) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//perhaps use intent if needed but i'm sure there's a specific intent action for up you can use to handle
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and inside your first activity's onCreate's intent handler for example :
if(Intent.ACTION_VIEW.equals(action))
you set the current page of the viewpager with viewpager.setCurrentItem(position). If you want to change the page for different items you could use intent.putExtra() in your second activity and then retrieve it in your first activity.
I have added the code below in my second activity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
return true;
}
It's working as I expected
is it a clean solution ?
Thanks

Cannot be cast the activity

I've got a little problem casting an activity and when I click the item in my menu to open the activity the app crashes and the logcat says that it can be cast the activity. The problem is in this line:
_Sapp = (SecondActivityApp)getApplicationContext();
I think it's the getApplicationContext but I'm not sure. How can I resolve it?
If you are trying to start a new Activity from the menu, you'll have to use an Intent just like you would, if you wanted to start an activity from any other place.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_testdone:
Intent intent = new Intent(getApplicationContext(), SecondActivityApp.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Android Actionbar Up button versus system Back button

I'm using the Actionbar and it's "up" button to return from a detail activity to the main activity, which works fine. Similarly, the user can press the system "back" button to return to the main activity.
In my main activity, in onCreate() data is downloaded from the internet to display upon app start. I noticed that when I use the Actionbar "up" button to go from detail to main activity, onCreate() is run, re-downloading the data. But onCreate() is not run when I use the system "back" button, therefore immediately showing the main activity view.
The code I use in the detail activity to implement the "up" button is:
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
I would like the "up" button to behave like the "back" button and not rerun onCreate(). But I'm unsure how to make this happen, or which code path the "back" button implements to return to the main activity.
Thanks!
Instead of starting a whole new activity simply finish the details activity you are in
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
Then you will return to the previous activity on the activity stack (your main activity) and onCreate shouldn't be called
If you want Up to do exactly what Back does, you can do this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Note that the default implementation of onBackPressed() just calls finish(), but onBackPressed can be overridden.
I think a better solution can be found in this post.
Calling finish() works in specific situations but may not always produce the behavior described in the documentation e.g:
By calling
Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(this, intent);
you'll return to the parent activity in the state in which you left it. If you have a flat app structure, it'll still act just like the Back button.
for a real "home" functionality , you should see the api demos ,under "App/Activity/Reorder Activities" .
the reason : what if you have something like this : activity1->activity2->activity3 , and now you wish to go to activity1 by pressing the home button on the action bar?
I believe the simplest way is to override the "getParentActivityIntent" method of the detail activity adding the flag "Intent.FLAG_ACTIVITY_CLEAR_TOP":
#Nullable
#Override
public Intent getParentActivityIntent() {
Intent intent = super.getParentActivityIntent();
if (intent != null) {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
return intent;
}
There is another solution which can be in hand for somebody. I had the same double-behavior when user pressed Back and ActionbarBack buttons. I was fine with Back btn behaviour. I didn't want an activity to be recreated. So I overrode the method
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
Works fine for me

Categories

Resources