Starting another activity without extending Activity superclass - android

I have a View class with some clickable bitmaps,in the onTouch method,i want to trigger a new activity when i have touched the bitmaps
Intent newintent = new Intent();
newintent.setClass(view.getContext(),MainMenu.class);
startActivity(newintent);
Since the class does not extends Activity,how can i start an activity without extending Activity?
the current error is :
The method startActivity(Intent) is undefined for the type DrawView

Provided that your MainMenu.class is an Activity and you call the startActivity() method from a View of some sort you need to add a Context from which your new Activity will be started.
In your case it would be:
view.getContext().startActivity(newintent);

You need to have a context to do that something like this should be okay.
Declare a context for your view at the head of your class.
Context myContext = view.getContext();
And then use it to start your activity.
Intent newintent = new Intent();
newintent.setClass(myContext,MainMenu.class);
myContext.startActivity(newintent);myContext.startActivity(newintent);

When you are initializing your View class. In the constructor you pass Context of your activity class e.g
View v = new View(context)
In you Own View class constructor. Make a reference of this context as class-level object.
public class MyView extend View{
private Context mContext = null;
MyView(Context context){
super(context);
mContext = context;
}
}
and when need to start a activity.
mContext. startActivity(newintent);

You can deliver the activity which the view in the show to this view.
The constructor of the view could be rewritten as:
View(Contenxt , ..)
And will use
context.start(...)

Related

Start activity from a AdapterRecyclerView

I have a viewPager with some fragments (in a fragment I have a recyclerView ), recyclerView contains more items, so, I want to when I click a item on recyclerView,it trans to BBBBActivity. But I don't figure out.
it errors
thank you very much
try this
Intent intent= new Intent(itemView.getContext(),addsongList.class);
itemView.getContext().startActivity(intent);
change your code like this or pass the Context in your adapter
Intent intent= new Intent(itemView.getContext(),AddSongToList.class);
getActivity.startActivity(intent);
You need a context to call startActivity(). Pass getActivity() as a context from your Fragment where you are initializing your adapter; like:
YourAdapter adapter = new YourAdapter(getActivity, /* other parameters*/);
In adapter:
private Context mContext;
YourAdapter(Context context, /*other parameters*/) {
mContext = context;
}
And then use mContext to call startActivity():
mContext.startActivity();

Clicking a link in a TextView that is part of an adapter in android

I am designing a chatting app where I am required to make links clickable inside a TextView. I have used android:autoLink="all" in TextView to distinguish links/phones etc.
This works up to displaying the part of TextView that are links/phones as clickable but when I click it, I get the following exception:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I searched a bit and it seems that I need to pass the activity context while inflating the TextView from XML. But how can I do that as i am inflating this view inside an adapter? This TextView is part of card view that is part of a recycler view in an adapter.
Instead of using layoutInflater.inflate() to inflate the view, I used LayoutInflater.from(parent.getContext()).inflate() and it worked.
Either use the Parent Activity to start a new activity from adapter or set the Intent flag.
For parent activity pass the Activity to Adapter's Construcutor and initialize it to use in the adatper like this.
in you MainActivity
Activity activity = this;
MyAdapter _adpater = new MyAdapter(....., activity);
in MyAdapter class
//constructor
Activity activity;
public MyAdapter (......, Activity _activity) {
........
this.activity= _activity;
}
now you can use this activity to start new activity.
activity.startActivity(new Intent(activity,target.class));
or you can try setting the flag with your current method.
context.startActivity(new Intent(context.getApplicationContext, target.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

How to refresh a view in main activity from an adapter?

I have a graph in main activity also I have a recycler view in main activity. Custom adapter is used for recyclerview. I have a check box and swipe layout in list item layout. in swipe layout there is a delete button.
I want to reset the graph of main activity when I check the check box or when I delete any item.
For this I created one method in main activity. And called this method in adapter onCheckedChangeListener and on click of delete.
But I am getting a null pointer exception on mBarChart. i.e . graph. I have instantiated in mBarChart in setUI method and this is called in onCreate of an activity.
resetMethod
public void resetGraph(Context context)
{
mBarChart.invalidate();
}
in adapter :
Context conext;
MainActivity mainActivity;
mainActivity = new MainActivity();
mainActivity.resetGraph(conext);
How to do this? Please help.. Thank you..
In Adapter call your resetMethod this way
((MainActivity)context).resetGraph(context);
Create a interface that implement Activity, Main activity in your case and override method and perform operation.
//Interface
public interface OnRefreshViewListner{
public void refreshView();
}
//Main Activity
MainActivity extends Activity implements OnRefreshViewListner
{
//Other methods
#Override
public void refreshView(){
// write refresh code here
}
}
//Initialize Interface in adapter constructor
public class YourAdapter extends BaseAdapter {
private OnRefreshViewListner mRefreshListner;
public YourAdapter (Context context) {
mRefreshListner = (OnRefreshViewListner)context;
}
//call MainActivity method
mRefreshListner.refreshView();
}
In the adapter, you should not create a new instance of MainActivity and call resetGraph(). You should use the instance of MainActivity, that created the adapter. Send the instance of MainActivity to the adapter, new Adapter(this) and save it in adapter.
You can change a view from the context of an adapter like this :
cast context to activity.
use findviewbyid method to find the view you want.
initiliaze it to a variable.
View v = ((Activity)getContext()).findViewById(WHATEVER_VIEW_COMPONENT_YOU_WANT);
change the variable as you want.
note. Don't forget to use the type of view that you want and cast the findview method to it.
If you want to call a method just cast the context to MainActivity and call it.

Access to getString() in android.support.v4.app.FragmentPagerAdapter?

In a class extending android.support.v4.app.FragmentPagerAdapter, is there any way to get access to the Context.getString(..) method without the extending class being an inner class of an activity or passing in some context from the outside?
Thanks for any hint!
From a fragment use :
getActivity().getString(...)
From an adapter use :
getContext().getResources().getString(...)
Yes, you need a context to access the resources.
From an Activity, use:
this.getString(R.string.string_name);
From a Fragment, use:
getActivity.getString(R.string.string_name);
From an adapter, use:
getContext().getResources().getString(R.string.string_name);
I had a similar issue. From a drawer layout, I wanted to decide which fragment to use in a method called from a helper class.
So in onCreateView...
String form = getResources().getStringArray(R.array.drawer_array)[i];
Context context = getActivity().getApplicationContext();
FragmentHelper fh = new FragmentHelper();
int myFragment = fh.getCurrentFragment(form,context);
And in public FragmentHelper()...
public int getCurrentFragment(String form, Context context){
...
context.getResources().getString(R.string.label_item1);
...
}
The trick being to add context in front of getResources(). Otherwise, my stack showed that the fragment was not attached to an activity.
Hope this helps someone.

How to access other class method from an Android class?

My app contains three classes: main Activity, View_A, View_B.
View_B needs to access a non-static method of View_A. How to do that ?
Both View_A and View_B have been initialized in the main activity in the onCreate method.
Thanks.
It is siple.You jst need to create the reference of need class in you current class and call the method with referenceedObject.methodName();
you could store View_A as a public instance variable within your Main Activity. then pass the context of main activity to View_B. You can then access the instance of View_A via the context of main activity within View_B.
Here's basically what i mean
This is in your main Activity:
Context context;
public View_A viewA;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
viewA = new View_A();
View_B viewB = new View_B(context);
}
it would probably be better using a getter method to get the viewA from context.
Here is View_B example class:
class View_B {
Context activityContext;
// constructor
View_B (Context _c)
{
activityContext = _c;
viewA = activityContext.viewA;
}
}
that is basically what i mean, but as i said in my comment, Teds solution seems more elegant.
I think he meant something more like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
View_A viewA = new View_A();
View_B viewB = new View_B();
// create a setter method in viewB class to set viewA instance into it after viewA & viewB are created. or i guess you could pass viewA to viewB in the constructor of viewB
viewb.setViewA(viewA);
}
I hope something from that can help you out
Basically, you need a reference to View_A inside View_B. One way to do that is to define a View_A variable in View_B that is accessible to the activity class. In onCreate, just set the variable to the instance of View_A once both View_A and View_B have been created.

Categories

Resources