I am newbie in app development.In my project have lot of Explicit Intent so i want to make a public method which argument contains conext or a class where i want to jump but it's not working is there have who know the proper solution for it.
Here is my method where i call my public intent class.
private void startActivityMethod() {
if (progressStatus == 100) {
if (firebaseUser != null) {
if (sp.getString(Constants.userType, "").equals("student")) {
Constants.explicitIntent(SplashActivity.this, studentMainActivity.class);
} else if (sp.getString(Constants.userType, "").equals("faculties")) {
Constants.explicitIntent(SplashActivity.this, facultiesMainActivity.class);
}
} else {
startActivity(new Intent(SplashActivity.this, LoginActivity.class));
finish();
}
}
}
Here is my public class method for intent.
public static void explicitIntent(Context context, Class<?> intentClass) {
Intent intent =new Intent(context,intentClass);
}
You should call startActivity to launch the intent
public static void explicitIntent(Context context, Class<?> intentClass) {
Intent intent =new Intent(context, intentClass);
context.startActivity(intent);
}
Related
public void changeActivity(Context context){
Intent intent =new Intent(this,context.getClass());
startActivity(intent);
finish();
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.register:
changeActivity(RegisterActivity);
break;
}
}
I am using a 'changeActivity' method but I got error in line 10. The error pointed that 'Expression Expected'
You're not passing your class file as object to Intent when you start activity.
Find out below solution :
public void changeActivity(Class<? extends Activity> context) { // Receive it here and provide to your intent.
Intent intent =new Intent(this, context);
startActivity(intent);
finish();
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.register:
changeActivity(RegisterActivity.class); // pass object of your desired activity as class parameter here
break;
}
}
You have pass an class object of your desination activity in your changeActivity method like this:
public void changeActivity(Class<? extends Activity> desinationActivity) {
Intent intent = new Intent(this, destinationActivity);
startActivity(intent);
finish();
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.register:
changeActivity(RegisterActivity.class);
break;
}
}
You can check out this link for reference. Hope this helps.
For writing this type of code you can check system source code. Following code of Intent constructor:
public Intent(Context packageContext, Class<?> cls) {
mComponent = new ComponentName(packageContext, cls);
}
I am working on an online radio app demo. I've created an error Activity which I want to take the user to, when an error occurs. In the error page, there is a refresh button, which is supposed to refresh the last Activity where an error occurred. But I don't know how to get the Intent of previous Activity which led to the error page to get it refresh on ButtonClick, I only know to make it return to a particular Activity.
You can use startActivityForResult in both calling activities
In MainActivity.java
int REFRESH = 1;
private void startErrorActivity() {
startActivityForResult(new Intent(this, ErrorActivity.class), REFRESH);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REFRESH) {
//do refresh
}
}
And in ErrorActivity.java
Button button = findViewById(R.id.refreshButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish(); //this will take you back to calling activities onActivityResult method
}
});
UPDATE:
I honestly think #sneharc's answer is better. Use that.
Try this:
public class ActivityA extends Activity {
public void myFunction(){
try{
// something bad happens here. need to go to ErrorActivity
}
catch (SomeException e){
Intent startErrorActivityIntent = new Intent(this, ErrorActivity.class);
startErrorActivityIntent.putExtra("sourceActivity", ActivityA.class.getSimpleName())
startActivity(this, startErrorActivityIntent)
}
}
}
public class ActivityB extends Activity {
public void myFunction(){
try{
// something bad happens here. need to go to ErrorActivity
}
catch (SomeException e){
Intent startErrorActivityIntent = new Intent(this, ErrorActivity.class);
startErrorActivityIntent.putExtra("sourceActivity", ActivityB.class.getSimpleName())
startActivity(this, startErrorActivityIntent)
}
}
}
public class ErrorActivity extends Activity {
private Intent mReceivedIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
mReceivedIntent = getIntent();
}
public void onClickRefresh(){
String retryActivityName = mReceivedIntent.getStringExtra("sourceActivity");
Intent retryActivityIntent = null;
if (!TextUtils.isEmpty(retryActivityName)){}
if (retryActivityName.equalsIgnoreCase(ActivityA.class.getSimpleName()))
retryActivityName = new Intent(this, ActivityA.class);
if (retryActivityName.equalsIgnoreCase(ActivityB.class.getSimpleName()))
retryActivityName = new Intent(this, ActivityB.class);
}
if (retryActivityIntent != null)
startActivityForResult(this, retryActivityIntent);
}
}
I'm trying to start an IntentService from a fragment tab but i have no responce. the code from my fragment is below:
private Intent prepareIntent(boolean isSending) {
Intent localIntent = new Intent(getActivity(), StartIActivity.class);
Log.d(THIS_FILE, "StartIActivity");
localIntent.putExtra("incoming", isSending);
localIntent.putExtra("remote_contact", setValidNumber(callUri));
localIntent.putExtra("acc_id", this.accId);
return localIntent;
}
private void startIAService(boolean bool) {
Log.d(THIS_FILE, "Start Service");
Context ctx = (Context) myFragment.this.getActivity();
ctx.startService(prepareIntent(bool));
return;
}
and my intent serviceclass is :
public class StartIActivity extends IntentService {
public StartIActivity() {
super("StartIActivity");
}
protected void onHandleIntent(Intent it) {
Intent intent = new Intent(it);
intent.setClass(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.d("IActivity", "Start Activity");
}
}
When run startIAservice the prepereIntent is run but it can't start the service. I need to use IntentService because i want to execute one task at a time but i can't understand how.
Any help here and what is the best code implemenattion to do this?
Declare service in manifest.xml
I want to start a new activity in non-Activity class that implements a DialogListener following is my code:
public class FacebookLoginDialog implements DialogListener {
#Override
public void onComplete(Bundle values) {
HomeActivity.showInLog(values.toString());
Intent i1 = new Intent (this, SearchActivity.class);
startActivity(i1);
}
#Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
#Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
#Override
public void onCancel() {
// TODO Auto-generated method stub
}
}
I can't start the new activity using intent in onComplete method, please help.
Thanks
This doesn't work because you need a Context in order to start a new activity. You can reorganize your class into something like this:
public class FacebookLoginDialog implements DialogListener {
private final Context context;
public FacebookLoginDialog(Context context) {
this.context = context;
}
#Override
public void onComplete(Bundle values) {
HomeActivity.showInLog(values.toString());
Intent i1 = new Intent (context, SearchActivity.class);
context.startActivity(i1);
}
//Other methods...
}
Then it will work.
Pass context as constructor parameter and then try this
Intent i = new Intent(this, SearchActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
use starActivity from non-activity class:
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "YOUR STRING");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share via...");
context.startActivity(Intent.createChooser(intent, "Share"));
For Easy Usage you can a method for this particular method:
public class Something
{
public static void navigate(Context context, Class<?> nameOfClass)
{
Intent i = new Intent(context, nameOfClass);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
can be called in other class and method everytime by calling this:
Something.navigate(activityName.this, classYourWantTONavigateTo.class);
How do you change activities using DroidGap? Is there a way to get the current context even though we're using DroidGap?
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isOnline() == true) {
super.loadUrl("http://myurl.com");
}
else {
Intent myIntent = new Intent(SOME CONTEXT...DroidGap.getContext(), LoadScreen.class);
startActivity(myIntent);
finish();
}
}
This solved my problem Intent myIntent = new Intent(getContext(), LoadScreen.class);