Starting a new FragmentActivity without content view shows non-transparent view - android

I open this simple fragmentActivity without setting any contentView
public class InternalDummyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
}
}
But I still see a blank view is opened. Why is that?
I have thought if there is no setContentView then the activity is transparent.
is it because of the fragmentActivity? How do I create a transparent fragmentActivity then?

Related

Instantiate a custom activity class with a ListView object from MainActivity

Unlike typical android projects starting with MainActivity with all the code of the layout object in it. This architecture requires me to have the initial code in a custom object. Here's a structure for better understanding.
java/MainActivity.java
java/User.java
layout/activity_main.xml
layout/user.xml
Now I also need a reference to User object within MainActivity and it looks like this.
public class MainActivity extends AppCompatActivity {
public Users users; // instantiate custom class and show
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My User class looks like this.
public class User extends AppCompatActivity {
ListView userList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
userList = view.findViewById(R.id.userList);
return view;
}
}
layout/user.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.puremvc.java.demos.android.employeeadmin.view.components.UserList">
<ListView
android:id="#+id/userList"
android:layout_width="395dp"
android:layout_height="715dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
</FrameLayout>
So, in other words, MainActivity just acts like a stage doing nothing except just providing a reference to the Initial object. Now I do need MainActivity to be there, can't point User to be a launcher in the manifest. Responsibilities are to be taken care of by User class.
Question: How to instantiate CustomClass User and show.
Context: The MainActivity class has to be minimalistic and clean, no User related code (ListView), all logic lies in the custom class.
P.S. There can be lateral approaches, as long as I have a reference to user Object in MainActivity and it's displayed on launch, I'll accept the answer.
As per my understanding: there should be two approaches.
First, by using the Fragment inside your Activity. Write all initialize and data flow codes inside the fragment and just initialize and start the fragment from the Activity. So when the Activity will start, it will give all its tasks to the fragment with its Context and rest thing Fragment will do.
Like below:
public class MainActivity extends AppCompatActivity {
public Users users; // instantiate custom class and show
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//the fragment stuff
FragmentManager fm= getFragmentManager() //or get SupportFragmentManager when the Fragment comes from Support lib
FragmentTransaction ft= fm.beginTransaction();
Fragment fragment= new UserListFragment();
ft.add(R.id.fragment_container, fragment);
ft.commit();
}
}
OR, the second approach should by using Interface and communicate both Activity and the Custom Class (or you can call it Controller) with it.
Its nothing, but a simple MVC design pattern which I never recommend.
You can write one Interface like below:
public interface IController{
public void initialize(Activity activity, Bundle savedInstanceState);
public void engage();
public void disengage();
}
Then, make an instance of this Controller inside your Activity/BaseActivity and use like below:
public MainActivity(IController controller){
this.controller = controller;
}
Then call each callback methods from their appropriate place to make them work inside the Controller.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//give the priviledge of onCreate to initialize
controller.initialize(this, savedInstanceState);
}
Then in your Controller class, just write the same program which you supposed to write inside Activity:
public class Your_Controller implements IController {
#Override
public void initialize(Activity activity, Bundle savedInstanceState) {
//do super where needed
//make one class level Activity instance to work in other methods
act = activity;
//just initialize views like below
TextView tv = (TextView) activity.findViewById(R.id.abc);
}

same Fragment Navigation Drawer in different activity

I used this link to create navigation drawer in my app. the question is :is there any way to create a basic activity witch have this drawer and extend other activity from this? i read this but all these links used DrawerLayout not Fragment Navigation Drawer and i can not use them. Is there any tutorial to solve my problem?
Here is what I do :
I create an abstrac class called RootActivity that extends Activity and which inflate the layout with the Drawer.
This class has an abstract method createPage in which you will inflate your activity layout.
Here is the basic code for the RootActivity :
public abstract class RootActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourRootlayout); //The root layout wich contain your Drawer
/**
* This FrameLayout is used has a container for your activity
* layout as you would do with a fragment container.
*/
FrameLayout container = (FrameLayout)findViewById(R.id.yourContainer);
View childActivityLayout = createPage(savedInstanceState);
if (childActivityLayout != null) {
container.addView(childActivityLayout);
}
}
public abstract View createPage(Bundle saveInstanceState);
}
And here is how you extend this root class :
public class ExampleActivity extends RootActivity {
#Override
public View createPage(Bundle saveInstanceState) {
View rootView = ...
//Inflate your layout
return rootView;
}
}

Android Activity, extend BaseActivity

What would be the correct way of implementing my idea? Should I use inflate?
Want to make aprox. 100 activities. All of them extending the same base-class (BaseActivity). What I want to accomplish is a BaseActivity that will show 3 View's (LeftView, MainView, RightView) in a LinearLayout (horizontal). This is not the problem. MainView is empty.
The problem arises when I want to design MyActivity (extends BaseActivity). My idea was that the R.layout designed in MyActivity ONLY was shown in MainView (part of BaseActivity).
Is this possible? and what would be the smartes/best way to implement this?
Kind regards, Ole
OK, the way I choose to handle this, was by inflating the R.layout:
public class BaseActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
return;
}
}
public class MyActivity extends BaseActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout da_layout_main = (RelativeLayout) findViewById( R.id.da_layout_main);
View view_child = getLayoutInflater().inflate( R.layout.my_activity, null);
da_layout_main.addView( view_child);
}
}

How to avoid repeating the same initialization code in every Activity's onCreate?

I made an abstract base activity called MyBaseActivity that extends Activity. I then extend MyBaseActivity for all of my concrete sub-activities. I did that so that I wouldn't have to set up the same menu for every single sub-activity.
However, I still have the following code repeated in all of my activities' onCreate() calls.
// Custom View for ActionBar
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View view = View.inflate(getApplicationContext(), R.layout.actionbar_top, null);
actionBar.setCustomView(view);
Is there a way to avoid repeating this code? Can I put it in MyBaseActivity? If so, do I need to send it the context? How would I do that?
Put it in your base Activity's onCreate().
In the subclass, when you call super.onCreate() the code will be executed.
public class BaseActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Custom View for ActionBar
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
View view = View.inflate(getApplicationContext(), R.layout.actionbar_top, null);
actionBar.setCustomView(view);
}
}
public class SubActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); // this calls BaseActivity's onCreate()
// at this point your actionbar custom view will have been set up
}
}

Fragment placed on top of NavigationDrawer

I'm trying to add a PreferenceFragmentin my application. The problem is, it's auto placed on top of my NavigationDrawer.
public class SetPreferenceActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
navigationDrawer(); // Loads the NavigationDrawer
getFragmentManager().beginTransaction().replace(android.R.id.content,
new Settings()).commit();
}
As you can see I load the "SettingsFragment" and replace the content with it? (I'm unsure) but it places it on top of everything else.. Here's my Settings fragment.
public class Settings extends PreferenceFragment {
static final String TAG = "MAIN";
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
Everything work as expected, BUT the PreferenceFragment are loaded in front, covering up the NavigationDrawer slideout, I tried calling bringToFront(); on the listview, with no luck.
A picture for reference :
Is it possible to tell the fragment to load behind the listview? I also tought about loading the fragment in a ViewPager, but I get an error that the Pager Adapter wont accept fragments of type PreferenceFragment.
Don't replace android.R.id.content, use the the id of the FrameLayout you have in the layout that contains your DrawerLayout.
In addition to what adneal said (in case others have the same problem):
The Activity which calls the PreferenceFragment needs to have the setContentView() method if you extend the Activity with your NavigationDrawer:
public class SetPreferenceActivity extends MyNavigationDrawer {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
getFragmentManager().beginTransaction().replace(R.id.drawer_frame_layout,
new Settings()).commit();
}
And the settings.xml should only contain a FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
I had same issue and i resolved it by replacing android.R.id.content to R.id.container.

Categories

Resources