PreferenceActivity onHeaderClick() doesn't work - android

I have been successfully using HoloEverywhere's PreferenceActivity for a while. I am now importing the SlidingMenu library and has been going well so far until I extended SlidingMenu's SlidingPreferenceActivity:
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingPreferenceActivity;
public class SettingsActivity extends SlidingPreferenceActivity{
...
and
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import org.holoeverywhere.preference.PreferenceActivity;
public class SlidingPreferenceActivity extends PreferenceActivity implements
SlidingActivityBase {
...
In my SettingsActivity I load some Header's to show the top level categories.
My problem is now onHeaderClick() is no longer working. I trace it all the way through and cannot find the error. Following the stack trace I see HoloEverywhere ends up making an Intent it eventually passes to the Android Activity, but I don't see anything that is obviously wrong.
What about the SlidingMenu Library could cause Fragments to stop working in a PreferenceActivity?

Turns out my problem had nothing to do with SlidingMenu. In my manifest I set all my Activitys to be singleInstance, so that I am not making multiple of the same Activity when I am navigating between them using the SlidingMenu. This was preventing the Intent that was created by PreferenceActivity when the Header was clicked from recreating the activity with the given fragment.
I removed the singleInstance reference for now and everything is working.

Related

How to extend a class from main application in an android library activity?

I've used this answer: Same Navigation Drawer in different Activities to implement a navigation drawer that is shown in all activities.
I am not sure if this is the right way to go, but at the moment I have a main application, that contains some activities and implements the NavigationDrawer in the BaseActivity.
I would like to start by creating many android library projects that will have activities/fragments in them and their own logic. So for now, besides the main application, I've created one android library project and I can successfully start one of its' activities from the main application (from the NavigationDrawer).
The problem is that I cannot make it extend the BaseActivity and therefore the NavigationDrawer is not shown.
So, at the moment in the main application, I have:
activity_main.xml // it is the launcher activity
activity_one.xml
activity_two.xml
MainActivity.java
OneActivity.java
TwoActivity.java
That works fine. Now I want to add the library project into play, in which I have:
activity_three.xml
ThreeActivity.java
And now the problem is that in the library project, I cannot extend from the BaseActivity like that:
public class ThreeActivity extends BaseActivity{ // <-- There's an error here: Cannot resolve symbol 'BaseActivity'
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
}
}
Does anybody have any suggestions how to do that?
Should I just create another library project that has the BaseActivity and the navigation drawer ?
Should I just create another library project that has the BaseActivity and the navigation drawer ?
Either that, or get rid of all the libraries and move everything into the one project. A library cannot inherit from classes from a project that consumes the library. A library can inherit from classes from a project that the library itself depends upon, though.
So, the app/ module could depend upon the three/ library (that has ThreeActivity), which in turn depends upon the base/ library (that has BaseActivity).

Change the default settings to create new projects

I have two questions here, both related.
When I create a new project with a blank activity in Android studio the project always creates an app with an ActionBarActivity.
public class MainActivity extends ActionBarActivity {
1.How can I change my project defaults to remove this default and just create a project as:
public class MainActivity
OR
to update it so its is not creating an app with deprecated code. Please see image.
Unfortunately there is no way to reconfigure the content of main activity created by default. Possibly the Google engineers will fix it some time in the future.
What you can do here to save some time is move the caret to the ActionBarActivity while being in the MainActivity.java file and hit Alt+Enter. The quick fix "Replace with AppCompatActiivty" will pop up and all you'll need to do is hit Enter to apply it. It's easier than replacing ActionBarActivity with AppCompactActivity by typing.
For 1. change to public class MainActivity extends Activity
For 2. change to public class MainActivity extends AppCompactActivity
EDIT: Creating project without activity.

Fragment Transaction not working when using AppCompatActivity or FragmentActivity

I have a typical application. An activity which has a FrameLayout and in this layout I want to switch between fragments. This is typically and easily done with:
getFragmentManager().beginTransaction()
.replace(R.id.ac_container, new FrOverview())
.addToBackStack(null)
.commit();
The problem is, that even if I use .addToBackStack(null) (And I know it's been added 'cause the stack count increases) when I press back I exit the application. I have been trying a lot of different code stuff and checked most threads here on Stackoverflow but I can't get it to work with code (method calls etc.).
But! I can get it to work, by changing the extended class of my activity class. If my class extends Activity, it works fine. But if I use AppCompatActivity (which in turn extends FragmentActivity) then it has the bad behaviour as explained earlier.
Feels like this has to be an error on Androids part, I am not doing anything wrong to my knowledge.
Does anyone have any suggestions on how to solve this? i.e. get the back functionality and keep the ActionBar!
AppCompatActivity uses the SupportFragmentManager, you need
to switch to SupportFragment and SupportFragmentManager

neokrees Navigation Drawer doesn't show up

As a newbie in Android programming I'm playing with some Material Design. So I wanted to implement a Navigation Drawer. After looking for libraries I decided to use #neokrees MaterialNavigationDrawer (https://github.com/neokree/MaterialNavigationDrawer). I added it into my build.gradle and followed the instructions from the wiki.
As a base I'm using a MainActivity which holds a toolbar and a fragment.
I created a custom style for the drawer and wrote a simple NavigationDrawer subclass.
import it.neokree.materialnavigationdrawer.MaterialNavigationDrawer;
public class NavigationDrawer extends MaterialNavigationDrawer {
#Override
public void init(Bundle savedInstanceState) {
this.addSection(newSection("Section", new MainActivity.PlaceholderFragment()));
}
}
Then I added this to AndroidManifest.xml
<activity android:name=".NavigationDrawer"android:theme="#style/MyNavigationDrawerTheme" />
So everything compiles fine, the app starts up but nothing happens. So as first try I added a section to the drawer, relating to an activity. As I saw that the first item should be a fragment, I replaced it accordingly. Then I looked into the example app, but I don't get it to compile. Anyway, there is also no drawer-relevant code in the MainActivity.
So it's most probably a really dumb failure, but I have no idea anymore. So I would be thankful for any help.

BaseActivity or Fragment Activity

Recently, I was placed on a project with another developer and he started our project with a BaseActivity (which extends ActionBarActivity), but when the app starts it runs MainActivity (which extends BaseActivity).
Activity Structure
BaseActivity = Parent
-- MainActivity = Child
-- AnotherActivity = Child
Wouldn't this be better practice to use MainActivity (which extends ActionBarActivity) and handle activity changes with Fragments?
Activity Structure
MainActivity = Parent
-- DrawerFragment = Child
-- GraphFragment = Child
P.S. There are many external internet API calls that should be done at the "Parent" Activity level.
Fragments are there for bigger displays like tablets. They allow you to display more than one fragment on the screen if it s big enough. It doesn't have anything to do with API calls. In my opinion it s easier to work with fragments, but you have to ask yourself if it s worth your time to implement it.

Categories

Resources