Passing getActivity() with ActionBarActivity - android

I have created my own search view as below
public class MySearchView extends SearchView {
public MySearchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
// The normal SearchView doesn't clear its search text when
// collapsed, so we will do this for it.
#Override
public void onActionViewCollapsed() {
setQuery("", false);
super.onActionViewCollapsed();
}
}
When i have to create an item of this search View i must pass the context like getActivity()
But since ActionBarActivity does not have getActivity(), what should I pass

Inside the ActionBarActivity extended class, you can access the context by calling the method: getApplicationContext().
Hope this helps!
Nick

Related

Use Activity method in Fragment class

I am new in Fragment and I am trying use Activity method in Fragment class. So please help me how I use Activity method in Fragment. And also how I use List<NameValuePair>, SharedPreferences in Fragment.
public class DashboardFragment extends Fragment {
public DashboardFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
return rootView;
}
This is Fragment code and I want to use Activity method in this. So please help I am new in this section. If there is another method, please tell me.
After the fragment's onAttach method is called (see Fragment lifecycle), you can obtain a reference to the activity through fragment's getActivity() method. Then you can cast this reference to the type of your activity and call the method:
((MyActivity)getActivity()).method();
Your activity method needs to be static and public. Then you could do following in your Fragment:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof MyActivity) {
this.activity = activity;
sharedPreferences = activity.getSharedPreferences(...);
}
}
Declare MyActivity activity in your Fragment.
then you can call activity.yourMethod() in another place. Dont forget to check if activity is null.
Use an interface implementation for this. Somethings like this:
Activity:
MyActivity extends AppCompatActivity implements CallBack {
#Override
public void onCallBack() {
// do something
}
}
And Fragment:
MyFragment extends Fragment {
public interface CallBack {
void onCallBack();
}
private CallBack callBack;
#Override
public void onAttach(Activity context) {
callBack = (CallBack) activity;
}
// when clicking or whatever it takes to call
callBack.onCallBack();
}
1) You can get Activity instance with getActivity() method in Fragment:
Activity activity = getActivity();
Then you can call any Activity method with the instance:
activity.method();
2) About SharedPreferences, you can also get Context instance with getContext() method in Fragment.
Context context = getContext();
So you can call getSharedPreferences() method with the instance:
SharedPreferences sharedPreferences = context.getSharedPreferences(String name, int mode);
3) List<T> is a pure Java class. So you can use it in Fragment without any limitation.

Applying adapter to ListFragment

I have a simple question.
just as we are able to apply adapter to ListActivity in the following way :
getListView().setAdapter(new myadapter());
the same does not work for ListFragment.
Can anybody update why this does not work apart from quoting from the api reference.
public class mylistfrag extends ListFragment
{
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListView lv=getListView();
ArrayAdapter<String> aa=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,((myfragshow)getActivity()).st);
lv.setAdapter(aa);
}
}
public class myfragshow extends FragmentActivity
{
String st[]={"a","b","C","d","e"};
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.myfragshow);
}
}
thanks
problem:
getListView()
when you are trying to call that method within your fragment it will call your activity's listview reference not your fragment's listview
here is the documentation:
Get the activity's list view widget.
instead directly use the setListAdapter to set the adapter of the fragment
sample:
setListAdapter((new myadapter()));
for a ListFragment you have to use ListFragment.setListAdapter() as if you use setadapter important initialization will be skipped.

Move from an activity to another one in View Class in Android

I have an activity that has two classes like these:
public class StartActivity extends Activity {
.
.
.
}
public class StartView extends View {
.
.
.
}
I want to go from this Activity to another one with click on one image.Is there any method (such startActivity method in Activity Class) in View Class that I can use it in the Second Class?
Do you mean something like this:
public class startView extends View {
Activity mActivity;
Context mContext;
public startView(Activity yourActivity, Context context, AttributeSet attrs) {
// TODO Auto-generated constructor stub
super(context, attrs);
this.mActivity = yourActivity;
this.mContext = context;
init();
}
public void init()
{
ImageView iv = (ImageView) findViewById(R.id.IMAGEVIEWID);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(mActivity, SecondActivity.class);
mContext.startActivity(i);
}
});
}
}

android inheritance activity(Base - Main activity)

I am having a problem about implemeting android apps.
I made baseActivity which is that base of other applications and other applications.
These are my code.
First, BaseActivity.java
public class BaseActivity extends Activity {
protected void onCreate(Bundle savedInstanceState,int layoutId) {
super.onCreate(savedInstanceState);
setContentView(layoutId);
Button menuBtn = (Button)findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("bss","menu");
}
});
}
}
And the other one is MainActivity.java
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState,R.layout.activity_main);
setContentView(R.layout.activity_main);
}
}
Now I have a question! When I was click the menu button, click listener did not act. It did not print log message and any action. So, I have a problem to make my application. Is it related with life-cycle or How can I solve the problem.
I do not like the way you are forcing thing, still if you want to make it works this way you have remove setContentView(R.layout.activity_main); from MainActivity, that's because one you call again setContentView(R.layout.activity_main); in the sublcass, the view hierarchy for that Activity will be recreated invalidating what you have done in the super class
The code is setting the Content View twice. Following is the sequence of your code.
setContentView()
Add button listener
setContentView()
Statement # 2, adds the button listener and it is all good till now. But as soon as you reset the content view on statement # 3, the previous settings get void. And the button gets reinitialized and the onClickListener is no more attached to the button.
Remove the contentview from BaseActivity and put
Button menuBtn = (Button)findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("bss","menu");
}
});
into MainActivity. The Base activity should be SuperClass and MainActivity subclass all the actions for view shold be initialize into the subclass.
Try this :
public class BaseActivity extends Activity {
protected void onCreate(Bundle savedInstanceState,int layoutId) {
super.onCreate(savedInstanceState);
}
public int getLayoutXML() {
return -1;
}
public abstract int getMenuId();
}
After this use this BaseActivity class like this :
public class service_detail extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use button like this:
Button menuBtn = (Button)findViewById(R.id.menuBtn);
menuBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("bss","menu");
}
});
}
#Override
public int getMenuId() {
// TODO Auto-generated method stub
return 1;
}
#Override
public int getLayoutXML() {
// TODO Auto-generated method stub
return R.layout.service_detail;
}
}

Method gets call on onBackPress in android

I am working on Base adapter in Android and i want to know that which overriden method gets call in BaseAdapter Class if i press onBackPress in some activity. Please help me, I have searched and didnt find any solution.
You probably have listview and you have a custom adapter set for the listview. Listview is in your activity.
class MyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstancestate)
{
setContentView(R.layout.main);
ListView lv= (ListView) findViewById(R.id.listview);
CustomAdapter cus= new CustomAdapter(MyActivity.this);
lv.setAdapter(cus);
}
}
class CustomAdapter extends BaseAdapter
{
....................
}
So when you press back button the current activity is popped form the back stack, destroyed and previous activity in the back stack takes focus.This the default behaviour.
http://developer.android.com/guide/components/tasks-and-back-stack.html
You can override onKeyDown(params) in your activity
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
//do something
finish();
return;
}
Overide onBackPressed in your activity and Change data to adapter accordingly then call notifyDataSetChanged
The onBackPressed will be activated in your Activity class. It is not an event of BaseAdapter class. So override your onBackPressed method in the Activity class.
you can pass activity reference to adapter via constructor like:
CustomAdapter customAdapter=CustomAdapter(getApplicationContext(),arrayList,HomeActivity.this);
and in Your Custom Adapter
public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<CategoryModel> arrCategoryModel;
AppCompatActivity activity;
public CustomAdapter(Context context, ArrayList<CategoryModel> arrCategoryModel, AppCompatActivity activity)
{
this.context=context;
this.arrCategoryModel=arrCategoryModel;
this.activity=activity;
} }
Now you can call activity.onBackPressed(); wherever you want.

Categories

Resources