Giter Site home page Giter Site logo

xyygit / pagerouter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liujingg/pagerouter

0.0 1.0 0.0 1.26 MB

A lightweight router framework for android 轻量级Android路由框架(支持Activity与Fragment跳转和参数注入)

Java 81.30% Kotlin 18.70%

pagerouter's Introduction

PageRouter

A lightweight router framework for Android applications.

Lastest version

module router router-compiler router-annotation
version Download Download Download

Demo

This repository also contains this demo project.

Demo

Feature

  1. Support activity jumps by standard uri
  2. Support fragment direct jumps by assign activity container
  3. Support multi router path
  4. Support multi-module
  5. Support automatic injection parameters
  6. Support add interceptor
  7. Support kotlin and parameters delegated

Add dependencies

    android {
        defaultConfig {
            ...
            //multi module configuration
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [targetModuleName: 'Other']// replace with the other module project name
                }
            }
        }
    }

    dependencies {
        // Replace the last version
        implementation "com.liujing.pagerouter:router:last-version"
        annotationProcessor "com.liujing.pagerouter:router-compiler:last-version"
        ...
    }

Kotlin project configuration

    apply plugin: 'kotlin-kapt

    // Replace the last version
    implementation "com.liujing.pagerouter:router:last-version"
    kapt "com.liujing.pagerouter:router-compiler:last-version"

Usage

1. Initialize the SDK

    // your application's specific scheme
    // main module's default initializer
    Router router = new Router(SCHEME, new AptRouterInitializer());

Register other module initializer or manually add router entry.

    router.register(new OtherRouterInitializer());
    router.register(new RouterInitializer() {
        @Override
        public void initActivityTable(Map<String, Class<? extends Activity>> router) {
            router.put("second2", SecondActivity.class);
        }

        @Override
            public void initFragmentTable(Map<String, Pair<Class<? extends Activity>, Class<? extends Fragment>>> router) {
        }
    });

2. Add annotations

Activity

    @RouterActivity({"second", "third"})
    public class SecondActivity extends AppCompatActivity {
        ...
    }

Fragment

    // The activityClazz here means the fragment currently associated with Activity
    @RouterFragment(value = "myfragment", activityClazz = FragmentContainerActivity.class)
    public class MyFragment extends Fragment {
        ...
    }

3. Parse parameters

    @RouterActivity({"second"})
    public class SecondActivity extends AppCompatActivity {
        @RouterField("id") // map parameters in the url by name
        private int id;
        ...

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_common);

            RouterArgsx.inject(this);// PageRouter will automatically set value of fields
            ...
        }
    }

4. Start routing

    Router.startActivity(context,Uri.parse("scheme://second?id=17"),null)

5. Add confusing rules

    -keep class * extends com.liujing.pagerouter.RouterInitializer { *; }

6. Processing jump results

    Router.startActivity(
       this,
       Uri.parse("scheme://second?id=17"), object : Callback {
            override fun onSuccess(context: Context, uri: Uri) {
                Toast.makeText(context, "success", Toast.LENGTH_SHORT).show()
       }
            override fun onFailed(context: Context, uri: Uri?, errorCode:Int, message: String?) {
                Toast.makeText(context, "failed : $message", Toast.LENGTH_SHORT).show()
       }
    })

7. From external jump

AndroidManifest.xml

    <activity android:name=".RouterCenterActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

            <data android:scheme="myscheme"/>
        </intent-filter>
    </activity>

Create a new Activity for monitoring scheme events

    public class RouterCenterActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Uri uri = getIntent().getData();
            if (uri != null) {
                Router.startActivity(this, uri, null);
            }
            this.finish();
        }
    }

8. Intercept jump process

   router.addInterceptor(new MyIntercept());
   class MyIntercept implements Interceptor {
        @Override
        public InterceptResult onIntercept(@NonNull Context context, @NonNull Uri uri) {
             ...
             return null;
             ...
             return InterceptResult.success();
             ...
             return InterceptResult.failed(errorCode, errorMessage);
        }
   }

License

Copyright(c)  2019 Liujing

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

pagerouter's People

Contributors

liujingg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.