Skip to content Skip to sidebar Skip to footer

How Start An Activity Using Intent Inside A Inner Static Class?

I have a static and inner class inside my Activityclass. I need to call an Activity using Intent(context,nameOfTheNewActivity.class) because I am trying to call an Intent when an i

Solution 1:

Try using the reference of the Activity.

ActivityOne.this.startActivity(intent);

If that doesn't work, then know that startActivity is a method of any Context.

classMessageViewHolderOfFriendextendsRecyclerView.ViewHolder {

    privatefinal Context context;

    publicMessageViewHolderOfFriend(View v) {
        super(v);
        context = v.getContext();

        v.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                Intentintent=newIntent(context,NewActivityToRun.class);
                context.startActivity(intent);
            }
        });

    }

Ref. How to open a different activity on recyclerView item onclick

Regarding

delete the word static from my inner class, but did not solve and the app crashes

You probably were closer to the solution with the removal of static. The app actually built. The app crashing means you should read the logcat and implement the proper solution.

Solution 2:

You need use the Context to start SecondActivity, when you are using context.startActivity(intent) and you should add intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) before startActivity(intent)

MainActivity.java :

publicclassMainActivityextendsAppCompatActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FloatingActionButtonfab= (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            // method 1:MyInnerClassinnerClass=newMyInnerClass();
            innerClass.firstLauncher(MainActivity.this);

           // method 2:// MyInnerClass.secondLauncher(MainActivity.this);
        }
    });
}


staticclassMyInnerClass {

    /**
     * member method
     */privatevoidfirstLauncher(Context context) {
        Intentintent=newIntent(context, SecondActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("flag_activity", "I'm 1th");
        context.startActivity(intent);
    }

    /**
     * static method
     */privatestaticvoidsecondLauncher(Context context) {
        Intentintent=newIntent(context, SecondActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("flag_activity", "I'm 2th");
        context.startActivity(intent);
    }

}
}

SecondActivity.java :

publicclassSecondActivityextendsActivity {

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    TextViewtextView= (TextView) findViewById(R.id.tv_msg);

    Stringmsg= getIntent().getStringExtra("flag_activity");
    textView.setText(msg);
}
}

All the implements are in the ContextImpl.java file.

Post a Comment for "How Start An Activity Using Intent Inside A Inner Static Class?"