Wednesday, 4 September 2013

Base Activity Class Example

It’s always been a good idea to make a baseActivity in android project.

Usually one screen is represented by an activity. Suppose, you have 3 custom activity classes (i.e, you writes them) in your android project. And you extended these 3 custom activity from android.app.Activity class.

What I am suggesting is to create an activity from android.app.Activity called baseActivity and make the 3 custom activity to extend from baseActivity.

1. You can avoid code duplication of you application wise common ui tasks. Suppose you want to show progressDialog in different activities. Just write code to show a progressDialog in your baseActivity and call that method from other activity.

2. Your application menu should be consistent across different application. For example, you have a settings screen. Option menu is containing the navigation to settings screen. You should display the settings throughout the application, means regardless of activity. User want to access the settings screen from anywhere in application. This feature can be easily achieved, but you have to override onCreateOptionsMenu in each of your activity or, you can override it once in your baseActivity.

res--->menu--> menu_common.xml

menu_common.xml:- 

           
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <item
     android:id="@+id/menu_settings"
     android:title="setting"
     android:showAsAction="always"
     />
   <item
     android:id="@+id/menu_gallery"
     android:title="gallery"
     android:showAsAction="always"
     />

</menu>


BaseClass.java :- 


 package com.shekhar.baseclassexample;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class BaseClass extends Activity{
                  @Override
                protected void onCreate(Bundle savedInstanceState) {
                    // TODO Auto-generated method stub
                    super.onCreate(savedInstanceState);
                }
                  public boolean onCreateOptionsMenu(Menu menu) {
                      getMenuInflater().inflate(R.menu.menu_common, menu);
              
                      // Get the SearchView and set the searchable configuration
                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                          SearchView searchView = (SearchView) menu.findItem(R.id.menu_gallery).getActionView();
                          searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
                          searchView.setIconifiedByDefault(true);
                      }
              
                      return super.onCreateOptionsMenu(menu);
                  }
              
                  @Override
                  public boolean onOptionsItemSelected(MenuItem item) {
                      int itemId = item.getItemId();
              
                      if (itemId == android.R.id.home) {
                          finish();
              
                      } else if (itemId == R.id.menu_settings) {
                          startActivity(new Intent(this, Setting.class));
              
                      } else if (itemId == R.id.menu_gallery) {
                          startActivity(new Intent(this,Gallery.class));
              
                      }
              
                      return super.onOptionsItemSelected(item);
                  }
              
               
                 
}





Home.java 

package com.shekhar.baseclassexample;

import android.os.Bundle;

public class Home extends BaseClass{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }

}

Setting.java:- 

package com.shekhar.baseclassexample;

import android.os.Bundle;

public class Setting extends BaseClass {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.setting);
        }
}

Out-Put:- 



 

 
 

No comments:

Post a Comment