overridePendingTransition on gridview adapter - android

How can I insert overridePendingTransition on a GridView `Adapter? In this way don't work, without transition startactivity work perfectly
bt.setOnLongClickListener(new OnLongClickListener(){
#Override
public boolean onLongClick(View v) {
final String selectedPad = Drum.pads[position];
Intent modPad = new Intent(v.getContext(), ModifyPad.class);
modPad.putExtra("pad", selectedPad);
context.startActivity(modPad);
overridePendingTransition(R.anim.exit_slid_in, R.anim.exit_slid_out);
return false;
}
});
I've read this post:
android start Activity in adapter (transition animiation direction problem), and comments related, but I don't know how pass the Activity in the Adapter. Any help?

Context is the Base Object of Activity ( see: What is the difference between Activity and Context? ),
so I used following:
Activity activity = (Activity) mContext;
activity.startActivity(repinIntent);
activity.overridePendingTransition(R.anim.act_start_in_from_right, R.anim.act_start_out_to_left);
Refers to: Getting activity from context in android

Related

ListView is leaking activity context

The following code is leaking the activity context:
This is actually inside an asyncTask in the onPostExecute
ChatCustomAdapter customAdapter = new ChatCustomAdapter(mContext, chatData, Typeface.createFromAsset(getAssets(), "font/Helvetica-Bold.ttf"));
mChatList.setAdapter(customAdapter);
inside the adapter the context is used for
inflater = LayoutInflater.from(mContext);
Am I holding a reference to the context? if so how do I release it?
LeakCanary is telling me that the ListView (mChatList) is leaking the context and if i remove setAdapter the leak has gone.
EDIT:
You can try to wrap your mChatList with WeakReference, for exp:
class ChatTask extends AsyncTask {
private WeakReference<ListView> mListRef;
public ChatTask(ListView chatList) {
mListRef = new WeakReference<ListView>(chatList);
}
public void onPostExecute() {
ListView chatList = mListRef.get();
if (chatList != null) {
Context context = chatList.getContext();
ChatCustomAdapter customAdapter = new ChatCustomAdapter(context, chatData,
Typeface.createFromAsset(context.getAssets(), "font/Helvetica-Bold.ttf"));
chatList.setAdapter(customAdapter);
}
}
}
If it still not work, you could try to follow this post
I assume that you create the inflater inside the ChatCustomAdapter constructor and keep that inflater as global variable to use later in getView method?
If that true, I think you should try to remove the variable inflater and inside getView method, create a local inflater by LayoutInflater.from(parentView.getContext);
Hope that helps.
ok so i have found the problem but i do not understand it so maybe some can comment and explain or answer with why it does this. the leak was in the Chat activity. to get to the Chat activity i have a button in the previous activity:
ChatButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Intent intent = new Intent(mContext, Chat.class);
mContext.startActivity(intent);
}
});
when i start the activity i was starting it from
mContext.startActivity(intent)
if i change this to just
startActivity(intent);
it doesn't leak.
Edit
it does still leak

android get activity context in abstract base class

I am creating an abstract base class to keep my navigation drawer code in one place and want to implement an onClickListener on my app title (defined in the toolbar) to start my launch activity
I am using the following code :
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.toolbar_title:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
return;
}
}
The app works properly, but I read somewhere that one must not use the Application context to start new activities. However, android studio doesn't let me use any other context apart from getApplicationContext and getBaseContext, maybe because this class is abstract.
Which context should I use then?
Have a look at Context.getApplicationContext() and ContextWrapper.getBaseContext(). Both have in common to be defined on a context instance. In your case it's even an Activity.
So you could even use this as a context to start your MainActivity. This is even better, because with any other context type you' have to include the flag FLAG_ACTIVITY_NEW_TASK to start a new activity.
If you get errors by using this for a context, it's because you define your OnClickListener as anonymous inner class which of course isn't a context. For that you'd have to write MyBaseActivity.this instead. This references the outer class instance.
Well, one of the ways can be: You can define an abstract method in your BaseActivity class:
abstract void launchMainActivity();
And call this method in your click listener:
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.toolbar_title:
launchMainActivity();
return;
}
}
The sub-classes can then implement this method as:
#Override
void launchMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}

Calling finish() inside onClickListener() of a TextView in SimpleAdapter?

Beginner Android and Java developer here.
I have this snippet of code inside a SimpleAdapter class I use to render a custom list containing a TextView and an ImageView. This is the part for the TextView which processes a URL in another activity.
for (int i = 0; i < fromList.length; i++) {
id = mySimpleAdapter.listMap.get(position).get("id");
holder.textView[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//How to call finish() here for containing Activity?
Intent i = new Intent (context, Details.class);
i.putExtra("url", "http://foo.net/do?id="+id);
context.startActivity(i);
}
});
}
My problem is how to simply call the finish() method of the Activity that contains this SimpleAdapter's linked ListView (to force the Activity to reload completely next time it is called).
Thanks to anyone who answers.
When instantiating the SimpleAdapter, you should pass it an Activity instance instead of Context, and then call finish() on it. Hope this helps.
It's quite simple to solve this problem.
First, you should declare an instance of activity
private Activity activity;
Then, you should "save" an instance of your activity in this variable. Do this inside the OnCreate method.
activity = this;
Then, just call, wherever you want, the function finish()
activity.finish();
Hope it helped you.

How to start one activity from Customized View

How to start one activity from another View (another activity View)
For example,
public class CorrectSmoothGloflo extends Activity {
.......................
setContentView(new Panel(this));
}
public class Panel extends View {
//This view class contains some drawable operation
// Here i want to start another Activity like this
Intent i=new Intent(CorrectSmoothGloflo.this,Screen.class);
startActivity(i);
}
I cant do this operation. Because this is the View, that will not work, because View does not have startActivity(). How to implement this? please give some guidelines.
Obtain a Context object and use its startActivity() method:
Context context = getContext();
Intent i = new Intent(context, Screen.class);
context.startActivity(i);
Setup an event handler to your "another activity View", and put the activity calling statements in it.
Intent i=new Intent(CorrectSmoothGloflo.this,Screen.class);
startActivity(i);
as you want to start another activity so u need to pass current context and not the previous like i your example your are mentioned correctsmoothgloflo but it is panel.class
check this is help for u or not...

New Activity nullpointerexception

I have a beginners problem. Here is my situation:
I want to start a new activity from the main activity. The code to launch the new activity is found in a separate class file. I seem to be passing the wrong arguments and I am ending up in a nullpointerexception when trying to launch the new activity. The new activity launches fine when I place the code in the main activity class file, therefore the second activity and the manifest are fine. Here is a sample of my code:
In my main activity class where I instanciate the second class (THIS IS MY MAIN ACTIVITY. I OMITTED THE REST BECAUSE I DO NOT THINK IT IS RELATED TO THE PROBLEM):
Tester mytest = new Tester();
mytest.test(this);
In my second class file (THIS IS NOT AN ACTIVITY; IT IS A CLASS THAT IS INSTANTIATED IN THE ACTIVITY):
public class Tester extends Activity {
Intent myIntent;
public void test (Context context) {
myIntent = new Intent (Intent.ACTION_VIEW);
myIntent.setClass(context, newActivity.class);
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
startActivity(myIntent);
}
}
):}
When I perform the click I receive a nullpointerexception at startactivity. Can anyone enlighten me on this please?I am sure that I am wrongly using the context.
Activities are started with Intents. Please read the Android Application Fundamentals first and try the Hello World app :)
I understood that you will use your separate Tester class at all cost ;) so I'm trying to adapt and help you out there.
First of all, don't let your class inherit from Activity. This won't help you, cause this calls will probably not have any valid context. Activity somehow implements the template pattern, providing you key method like onCreate(...), onPause(...) etc and is instantiated by the Android OS.
If you still want to use the class, you have to pass in the context. Probably you're aiming for some MVC/MVP pattern structure, anyway.
public class Tester {
private Context context;
public Tester(Context context){
this.context = context;
}
public void test () {
final Intent myIntent = new Intent(context, NewActivity.class);
//guess this comes from somewhere, hope through a findViewById method
thebutton.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
context.startActivity(myIntent);
}
}
)};
}
}
This would be a proposed solution from my side. A problem I still see here is on how you retrieve the button in that test() method. In order to have that work properly you have to retrieve it from some View class (with view.findViewByid(R.id.myButton)) or to create it dynamically and associate it with the view during the onCreate(...) of your Activity (probably using an Inflater).

Categories

Resources